首页 > 解决方案 > Using method from another class in Xamarin

问题描述

I am using Xamarin Cross Platform and Windows 10. Trying to add behavior on entry. Had class Behaviors:

using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;

namespace CoTraveller
{
    public class Behaviors
    {
        public class NumericValidationBehavior : Behavior<Entry>
        {
            protected override void OnAttachedTo(Entry entry)
            {
                entry.TextChanged += OnEntryTextChanged;
                base.OnAttachedTo(entry);
            }

            protected override void OnDetachingFrom(Entry entry)
            {
                entry.TextChanged -= OnEntryTextChanged;
                base.OnDetachingFrom(entry);
            }

           public void OnEntryTextChanged(object sender, TextChangedEventArgs args)
            {
                double result;
                bool isValid = double.TryParse(args.NewTextValue, out result);
                ((Entry)sender).TextColor = isValid ? Color.Default : Color.Red;
            }
        }
    }
}

Xaml page where I want to use this class method:

<?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:CoTraveller"
             x:Class="CoTraveller.RegistrationPage">
    <ContentPage.Content>
        <ScrollView>
        <StackLayout VerticalOptions="CenterAndExpand" Padding="5">
            <StackLayout Orientation="Horizontal">
                    <Entry x:Name="name_entry" Placeholder="First Name">
                        <Entry.Behaviors>
                            <local:NumericValidationBehavior />
                        </Entry.Behaviors>
                    </Entry>
                    <Entry x:Name="surname_entry" Placeholder="Surname"></Entry>
            </StackLayout>
            <Entry Placeholder="Login"></Entry>
                <StackLayout Orientation="Horizontal">
                    <Entry x:Name="pass_entry" Placeholder="Password" IsPassword="True"></Entry>
                    <Entry x:Name="pass_confirm_entry" Placeholder="Confirm Password" IsPassword="True"></Entry>
                </StackLayout>
                <StackLayout Orientation="Horizontal">
                    <Label Text="Save Password"></Label>
                    <Switch IsToggled="False"></Switch>
                </StackLayout>
                <Label Text="Date Of Birth"></Label>
                <DatePicker x:Name="birthday_dp"></DatePicker>
                <Picker x:Name="sex_p" HorizontalOptions="FillAndExpand" Title="Click and select gender">
                <Picker.Items>
                    <x:String>Male</x:String>
                    <x:String>Female</x:String>
                </Picker.Items>
            </Picker>
            <Entry  Placeholder="Email (optional)"></Entry>
                <Entry Placeholder="Phone number (optional)"></Entry>
                <Picker x:Name="type_entry" HorizontalOptions="FillAndExpand" Title="Click and select user type">
                <Picker.Items>
                    <x:String>Driver</x:String>
                    <x:String>Pedestrian</x:String>
                </Picker.Items>
            </Picker>
            <Button Text="Sign Up" Clicked="SignUpBtn"></Button>
            <Label x:Name="sign_in_lbl" Text="Already have account? Sign In" TextColor="Blue"></Label>
        </StackLayout>
        </ScrollView>
    </ContentPage.Content> </ContentPage>

Both are in the same namespace and CoTraveller is the name of my project. I had errors like: Severity Code Description Project File Line Suppression State Error Position 12:30. Type local:NumericValidationBehavior not found in xmlns clr-namespace:CoTraveller CoTraveller D:\CoTraveller\CoTraveller\CoTraveller\RegistrationPage.xaml 12

Severity Code Description Project File Line Suppression State Error XLS0414 The type 'local:NumericValidationBehavior' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. CoTraveller RegistrationPage.xaml 12

What is the reason? I declared namespace in XAML file, but smth is wrong...

标签: xamlxamarin

解决方案


The namespace declaration xmlns:local="clr-namespace:CoTraveller" is correct, but because the behavior is a nested type, you need to qualify the name with the outer type where you use it. Use <local:Behaviors.NumericValidationBehavior />.


推荐阅读