使用Xamarin中另一个类的方法

时间:2018-07-21 18:39:58

标签: xaml xamarin

我正在使用Xamarin Cross Platform和Windows 10。 尝试添加条目行为。发生过行为:

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页面:

<?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>

两者都在同一个名称空间中,而CoTraveller是我的项目的名称。 我有类似的错误: 严重性代码说明项目文件行抑制状态 错误位置12:30。在xmlns clr-namespace中找不到本地类型:NumericValidationBehavior:CoTraveller CoTraveller D:\ CoTraveller \ CoTraveller \ CoTraveller \ RegistrationPage.xaml 12

严重性代码描述项目文件行抑制状态 错误XLS0414找不到类型'local:NumericValidationBehavior'。验证您没有丢失程序集引用,并且所有引用的程序集均已构建。 CoTraveller RegistrationPage.xaml 12

是什么原因?我在XAML文件中声明了名称空间,但是smth是错误的...

1 个答案:

答案 0 :(得分:1)

名称空间声明xmlns:local="clr-namespace:CoTraveller"是正确的,但是由于该行为是嵌套类型,因此您需要使用外部类型来限定名称。使用<local:Behaviors.NumericValidationBehavior />