Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将TextField绑定到Xamarin.Forms中的Int属性_Xamarin.forms_Data Binding - Fatal编程技术网

将TextField绑定到Xamarin.Forms中的Int属性

将TextField绑定到Xamarin.Forms中的Int属性,xamarin.forms,data-binding,Xamarin.forms,Data Binding,我的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:

我的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>
但是,当我运行应用程序时,条目的默认状态为空,而不是值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 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访问私有属性,因为它是页面的实例,所以您不能从实例访问私有属性。
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;
    }
}