[英]Xamarin.forms - 3 pickers with 3 different change methods gets confused
I have these 3 Pickers:我有这 3 个选择器:
<Picker x:Name="CommunityPicker" Title="Select Community" WidthRequest="400" ItemDisplayBinding="{Binding Description}" SelectedIndexChanged="communityChange" />
<Picker x:Name="LotPicker" Title="Select Lot" WidthRequest="400" ItemDisplayBinding="{Binding Lot}" SelectedIndexChanged="lotChange" />
<Picker x:Name="ModelPicker" Title="Select Model" WidthRequest="400" ItemDisplayBinding="{Binding Description}" SelectedIndexChanged="modelChange" />
and I have these 3 changes methods:我有这 3 种更改方法:
private void communityChange(object sender, EventArgs e) {
community = ((sender as Picker).SelectedItem as AreaClass).Area;
}
private void lotChange(object sender, EventArgs e) {
lot = ((sender as Picker).SelectedItem as LotClass).Lot;
}
private void modelChange(object sender, EventArgs e) {
model = ((sender as Picker).SelectedItem as ModelClass).Model;
}
When I go to use the Pickers for the first time everything works, but when I goto pick another option, I get the following error:当我第一次使用选择器时,一切正常,但是当我选择另一个选项时,出现以下错误:
Object reference not set to an instance of an object你调用的对象是空的
On the lines where I am getting the selected value....and for some reason I am getting this error on the wrong method...IE when I goto change the value of CommunityPicker, I am getting the method for LotPicker...在我获取选定值的行上....出于某种原因,我在错误的方法上遇到此错误...IE 当我更改 CommunityPicker 的值时,我正在获取 LotPicker 的方法...
What am I doing wrong?我究竟做错了什么?
I don't know the other codes of your app.我不知道你的应用程序的其他代码。 But I reproduced this question by a simple demo.You can check it here .但是我通过一个简单的演示重现了这个问题。你可以在这里查看。
And I resolved this question by determining whether the value is null and then process it accordingly, you can refer to it:而我通过判断该值是否为空来解决这个问题,然后进行相应的处理,大家可以参考:
private void RegionPicker_SelectedIndexChanged(object sender, EventArgs e)
{
Region region = ((sender as Picker).SelectedItem as Region);
if (region != null)
{
StreetPicker.ItemsSource = region.Streets;
}
else {
StreetPicker.ItemsSource = null;
}
}
So you can try to do like this(The same goes for other pickers):所以你可以尝试这样做(其他选择器也是如此):
LotClass temp = (sender as Picker).SelectedItem as LotClass);
if(temp!=null){
}else{
}
Hope it can help you.希望它可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.