【发布时间】: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