0

I am creating a xamarin behaviour to validate an email id, therefore I created the behaviour file and tried to localise it in XAML file but I get the below error

Xamarin.Forms.Xaml.XamlParseException: Position 12:10. Type local:EmailBhvr not found in xmlns clr-namespace:Validation.Helpers;assembly=Validation.Helpers

Namespace: Validation

Behaviour Code File: EmailBhvr

Here is my XAML code:

<? xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns = "http://xamarin.com/schemas/2014/forms"
                xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                xmlns:local="clr-namespace:Validation;assembly=Validation"
                x:Class="Validation.WelcomePage">
    <StackLayout>
    <Label Text = "Hello Xamarin" />


    < Entry Placeholder="Enter your full name" x:Name="myName">
        <Entry.Behaviors>
        <local:EmailBhvr />
        </Entry.Behaviors>
    </Entry>

    <Entry Placeholder = "Enter your email" x:Name="myEmail" />

    <Entry Placeholder = "Enter password" x:Name="myPassword" IsPassword="True"  />

    <Entry Placeholder = "Confirm password" x:Name="myConfirmPassword" IsPassword="True"  />

    <Button Text = "Save" x:Name="SaveRegistration"/>
    </StackLayout>
</ContentPage>
3
  • I just ran this through mfractor's Xaml analyser (docs.mfractor.com/xamarin-forms/tools-in-depth/analysers); What is <local:EmailBhvr /> and is it defined in the Validation assembly and namespace? Commented Feb 22, 2017 at 16:49
  • EmailBhvr is the behaviour class for validating the email id Commented Feb 23, 2017 at 6:07
  • make sure that EmailBhvr Class is having the same namespace that you have written in xmlns:local Commented Sep 20, 2017 at 7:10

5 Answers 5

3

This could be related to a known linking issue - where Xamarin compiler ends up linking out classes (from external assemblies) that have references only in XAML.

Looks like EmailBhvr might be getting linked out by the compiler. There are couple of links that talk about this:

  1. Extending Control plugins > Getting Started
  2. Force assembly linking

There are a lot of options to resolve this:

  1. Add a static Init method in each class as mentioned here in "Getting started" section here

    // this ensures the class does not get 
    // linked out in the application we add this assembly to.
    public static void Init() { }
    
  2. Or, preserve code using preserve attributes on Android, and iOS

    public class Example
    {
         [Android.Runtime.Preserve]
         public Example ()
         {
         }
    }
    
  3. Or, use Custom linking.

  4. Or, update project configuration to not link. Android, and iOS. Not a recommended option though.

Sign up to request clarification or add additional context in comments.

Comments

1

I feel your XAML is clean. Looks error free. I think the problem is with the EmailBhvrclass in Validation. I suggest you to verify it. Make sure that the assembly name in XAML is also correct. XamlParseException can also occur with the incorrect assembly name..

Comments

0

I think you made some mistake while writing tags/property names in your EmailBhvr file.

Because of that you are getting parsing exception.

Comments

0

For the import to work, the class EmailBhvr must

  1. be named "EmailBhvr"
  2. reside in an assembly with an assembly name "Validation".
  3. reside in the namespace "Validation" (check your class file)

Be especially careful with 2.: If you use a shared Project the assembly name will be that of the platform project (e.g. it could be Validation.Droid / Validation.iOS). That can be fixed be giving both the same Assembly name (in project properties). For example "Validation.Platform" and change the xaml namespace import accordingly

Comments

0

Please Make sure that the assembly name in XAML is correct. it should be like this xmlns:local="clr-namespace:ProjectNamspace.Validation;assembly=ProjectNamspace.Validation"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.