获取自定义条目的文本

时间:2019-08-28 14:16:32

标签: xamarin xamarin.forms xamarin.android xamarin.ios

我做了一个自定义条目,但问题是:现在我不知道如何从该组件中获取文本,条目x:name.text无法正常工作

我尝试将其放入自定义类中,但未成功:

    public new string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }

我的contentpage.cs:

    async void LoginUser(object sender, EventArgs e)
            {
                if (string.IsNullOrEmpty(usernameEntry.Text) || string.IsNullOrEmpty(passwordEntry.Text))
                {
                    if (string.IsNullOrEmpty(usernameEntry.Text))
                        await DisplayAlert("Usuario", "Digite o Usuario", "OK");
                    else
                        await DisplayAlert("Senha", "Digite a Senha", "OK");
                    return;
                }

我的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:custom="clr-namespace:HCTaNaMao.Customs"
             x:Class="HCTaNaMao.Views.Login" >   
    <ContentPage.Content>
        <StackLayout VerticalOptions="FillAndExpand" Padding="0,100,0,0">
            <Image Source="HCbackground.png" VerticalOptions="Center" HeightRequest="200" />
            <Label Text="Usuario" HorizontalTextAlignment="Center"/>
            <custom:CustomEntry
                                x:Name=" usernameEntry"
                                CornerRadius="18"
                                IsCurvedCornersEnabled="True"
                                BorderColor="LightBlue"    
                                HorizontalTextAlignment="Start"
                                FontSize="17"
                                HeightRequest="40"
                                Placeholder="Usuário"
                                PlaceholderColor="LightGray"
                                TextColor="Black"
                                FontAttributes="Bold"
                                WidthRequest="100"/>
            <Label Text="Senha"  HorizontalTextAlignment="Center"/>
            <custom:CustomEntry
                                x:Name=" passwordEntry"
                                CornerRadius="18"
                                IsCurvedCornersEnabled="True"
                                BorderColor="LightBlue"    
                                HorizontalTextAlignment="Start"
                                FontSize="17"
                                HeightRequest="40"
                                Placeholder="Senha"
                                PlaceholderColor="LightGray"
                                TextColor="Black"
                                FontAttributes="Bold"
                                WidthRequest="100"
                                IsPassword="True"/>

            <Button Text="Entrar" TextColor="White" Clicked="LoginUser" WidthRequest="110" 
            HorizontalOptions="Center" BackgroundColor="SteelBlue" BorderRadius="20"/>
            <Label x:Name="messageLabel" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

我的自定义类:


    public class CustomEntry : Entry
        {
            public static readonly BindableProperty BorderColorProperty =
            BindableProperty.Create(
                nameof(BorderColor),
                typeof(Color),
                typeof(CustomEntry),
                Color.Gray);

            // Gets or sets BorderColor value
            public Color BorderColor
            {
                get { return (Color)GetValue(BorderColorProperty); }
                set { SetValue(BorderColorProperty, value); }
            }


            public static readonly BindableProperty BorderWidthProperty =
            BindableProperty.Create(
                nameof(BorderWidth),
                typeof(int),
                typeof(CustomEntry),
                Device.OnPlatform<int>(1, 2, 2));

            // Gets or sets BorderWidth value
            public int BorderWidth
            {
                get { return (int)GetValue(BorderWidthProperty); }
                set { SetValue(BorderWidthProperty, value); }
            }


            public static readonly BindableProperty CornerRadiusProperty =
            BindableProperty.Create(
                nameof(CornerRadius),
                typeof(double),
                typeof(CustomEntry),
                Device.OnPlatform<double>(6, 7, 7));

            // Gets or sets CornerRadius value
            public double CornerRadius
            {
                get { return (double)GetValue(CornerRadiusProperty); }
                set { SetValue(CornerRadiusProperty, value); }
            }


            public static readonly BindableProperty IsCurvedCornersEnabledProperty =
            BindableProperty.Create(
                nameof(IsCurvedCornersEnabled),
                typeof(bool),
                typeof(CustomEntry),
                true);

            // Gets or sets IsCurvedCornersEnabled value
            public bool IsCurvedCornersEnabled
            {
                get { return (bool)GetValue(IsCurvedCornersEnabledProperty); }
                set { SetValue(IsCurvedCornersEnabledProperty, value); }
            }

            public new string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }

usernameEntry.Text为空


    System.NullReferenceException: Object reference not set to an instance of an object.

1 个答案:

答案 0 :(得分:0)

您有:

  public new string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }

不应使用新标签。

但是,Entry已经具有Text属性,并且您正在扩展CustomEntry的Entry类。因此,简单地说,将其删除:

  public new string Text
            {
                get { return (string)GetValue(TextProperty); }
                set { SetValue(TextProperty, value); }
            }

您将可以输入文字。