【问题标题】:Binding TextField to Int property in Xamarin.Forms将 TextField 绑定到 Xamarin.Forms 中的 Int 属性
【发布时间】:2020-06-22 19:19:58
【问题描述】:

我的 XAML 中有一个步进器和一个数字条目:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:views="clr-namespace:brahms.View"
             x:Class="brahms.LabelRequestPage"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
             Title="Request Labels"
             ios:Page.UseSafeArea="True">
    <ContentPage.Resources>
        <ResourceDictionary>
            <views:IntConverter x:Key="IntToString"/>
        </ResourceDictionary>
    </ContentPage.Resources>
    <ContentPage.Content>
        <StackLayout>
            <StackLayout Orientation="Horizontal">
                <Label Text="Number of labels:"/>
                <Entry Text="{Binding Path=LabelQuantity, Mode=TwoWay, Converter={StaticResource IntToString}}" Keyboard="Numeric"/>
                <Stepper Value="{Binding LabelQuantity}" />
            </StackLayout>
            <Button Text="Cancel" Clicked="CancelButton_Clicked"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

并且代码隐藏中的关联属性调用OnPropertyChanged

namespace brahms
{
    public partial class LabelRequestPage : ContentPage
    {
        private int _quantity;
        private int LabelQuantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                _quantity = value;
                OnPropertyChanged("LabelQuantity");
            }
        }

        public LabelRequestPage()
        {
            LabelQuantity = 1;
            InitializeComponent();
        }

        void CancelButton_Clicked(System.Object sender, System.EventArgs e)
        {
            Navigation.PopModalAsync();
        }
    }
}

但是,当我运行我的应用程序时,条目的默认状态是空的,而不是值 1。步进器不会更改条目,并且在条目中设置值不会改变步进器的位置(如果您计算向上点击 + 和 - 来计算它的当前值)。

我的结论是这两个控件没有绑定到代码隐藏中的属性,所以我的问题是为什么不呢?我需要做什么才能将这些控件绑定到该属性?

【问题讨论】:

    标签: xamarin.forms data-binding


    【解决方案1】:

    你可以这样做

    <StackLayout Orientation="Horizontal" VerticalOptions="CenterAndExpand">
                <Label Text="Number of labels:"/>
                <Entry VerticalOptions="Start" HorizontalOptions="FillAndExpand" Text="{Binding LabelQuantity, Mode=TwoWay, Converter={StaticResource IntToString}}" Keyboard="Numeric"/>
                <Stepper Value="{Binding LabelQuantity,Mode=TwoWay}" />
     </StackLayout>
    

    后面的代码应该是这样的。

    public partial class LabelRequestPage : ContentPage
    {
        public LabelRequestPage()
        {
            InitializeComponent();
            BindingContext = this;
            LabelQuantity = 1;
        }
    
        private int _quantity;
        public int LabelQuantity
        {
            get
            {
                return _quantity;
            }
            set
            {
                _quantity = value;
                OnPropertyChanged(nameof(LabelQuantity));
            }
        }
    }
    

    对于转换器,您可以使用它。

    public class IntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is int)
                return ((int)value).ToString();
            else
                return 0;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int number = 0;
            int.TryParse(value.ToString(), out number);
            return number;
        }
    }
    

    【讨论】:

    • 谢谢,这行得通,最初它没有,但我没有注意到您在LabelQuantity 上将属性可见性从private 更改为public。为什么需要这个? XAML 实现了与代码隐藏相同的类的一部分,为什么它不能访问私有属性?
    • 您无法从 BindingContext 访问私有属性,因为它是页面的实例,因此您无法从实例访问私有属性。
    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多