选择Xamarin DisplayActionSheet,在标签中显示值
我已经将ContentPage连接到一个类(g)的实例上,这个例子运行良好:选择Xamarin DisplayActionSheet,在标签中显示值,xamarin,Xamarin,我已经将ContentPage连接到一个类(g)的实例上,这个例子运行良好: 打开页面 在输入框中输入值 从DisplayActionSheet中进行选择 单击保存 OnSave UI中的所有值都是g,但是DisplayActionSheet中的值不在我期望的UI中 在DisplayActionSheet运行之后,我希望在UI中显示aisledephtext的值 下面是我实例化为变量g的类 public class GroceryItemForSaving { public Groc
- 打开页面
- 在输入框中输入值
- 从DisplayActionSheet中进行选择
- 单击保存
public class GroceryItemForSaving
{
public GroceryItemForSaving() { }
public event PropertyChangedEventHandler PropertyChanged;
private string _AisleDepth;
public string AisleDepth
{
get
{
return _AisleDepth;
}
set
{
_AisleDepth = value;
OnPropertyChanged();
}
}
private string _AisleDepthText;
public string AisleDepthText
{
get
{
return _AisleDepthText;
}
set
{
_AisleDepthText = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
我将g作为BindingContext,如下所示:
public NewGrocery()
{
InitializeComponent();
BindingContext = g;
}
public async void ShowAisleDepthChoices(object sender, EventArgs e)
{
var AisleDepth = 0;
var SelectedAisleDepth = await DisplayActionSheet("Aisle Depth", "Cancel", null, "Front", "Middle", "Back", "Back Wall");
switch (SelectedAisleDepth)
{
case "Front":
AisleDepth = 1;
break;
case "Middle":
AisleDepth = 2;
break;
case "Back":
AisleDepth = 3;
break;
case "Back Wall":
AisleDepth = 4;
break;
}
g.AisleDepthText = SelectedAisleDepth;
g.AisleDepth = AisleDepth.ToString();
}
下面是相关的XAML
<Label Text="GroceryName" Grid.Row="0" Grid.Column="1" ></Label>
<Entry Text="{Binding GroceryName}" Grid.Row="0" Grid.Column="2" ></Entry>
<Label Text="Aisle" Grid.Row="1" Grid.Column="1" ></Label>
<Entry Text="{Binding Aisle}" Grid.Row="1" Grid.Column="2"></Entry>
<Label Text="Aisle Depth" Grid.Row="2" Grid.Column="1" ></Label>
<Label Text="{Binding AisleDepthText, Mode=OneWay}" Grid.Row="2" Grid.Column="2" ></Label>
<Button Clicked="ShowAisleDepthChoices" Grid.Row="2" Grid.Column="3" Text="Aisle Depth" ></Button>
然后,在AISLEDEPHTEXT标签中不显示任何值,但当我单击“保存”时,这些值在g.ASILEDEPHTEXT和g.AisleDept中的位置正好是我所期望的位置。注意:我可以直接在UI中输入一个GroceryName,并在保存时以g.GroceryName结束
在DisplayActionSheet完成操作后,我需要做什么才能使g.aisledephtext的值显示在UI中?GroceryItemforSave需要实现INotifyPropertyChanged。您必须编写代码以满足实现,但您没有声明类使用接口,因此绑定没有像它应该的那样更新
public class GroceryItemForSaving : INotifyPropertyChanged
谢谢我忘记了那里有继承/接口的东西。我猜我(无意识地)假设,如果INotifyPropertyChanged不在应该发生的位置,则属性上的OnPropertyChanged()应该引发异常。