Windows phone 如何在windows phone 8中执行基于ListPickerItem选择的功能
我的xaml代码中有一个ListPicker,它包含的不止ListPickerItem,我想在地图上显示一个图钉,根据它选择ListPickerItem 这是我的XAML:Windows phone 如何在windows phone 8中执行基于ListPickerItem选择的功能,windows-phone,listpicker,Windows Phone,Listpicker,我的xaml代码中有一个ListPicker,它包含的不止ListPickerItem,我想在地图上显示一个图钉,根据它选择ListPickerItem 这是我的XAML: <toolkit:ListPicker Foreground="white" Opacity="0.9" x:Name="OptionSelection" Margin="0,18,0,0" SelectionChanged="Picker"> <toolkit:Li
<toolkit:ListPicker Foreground="white" Opacity="0.9" x:Name="OptionSelection" Margin="0,18,0,0" SelectionChanged="Picker">
<toolkit:ListPickerItem Tag="1" x:Name="Option1" Content="Item1"/>
<toolkit:ListPickerItem Tag="2" x:Name="Option2" Content="Item2"/>
<toolkit:ListPickerItem Tag="3" x:Name="Option3" Content="Item3"/>
</toolkit:ListPicker>
我主要想知道如何在我的代码上应用if语句,这将帮助我添加基于所选项目的地理定位功能。您基于用户选择执行代码的
if
语句看起来很好,除了在本例中标记的值是一个字符串,所以您应该将其与另一个字符串进行比较(“1”
)而不是整数(1
)
当SelectedItem
的值为null时,似乎会引发异常。您可以尝试在函数的开头添加简单的检查,以正确处理此条件并避免NullReferenceException
:
private void Picker(object sender, SelectionChangedEventArgs e)
{
if(OptionSelection.SelectedItem == null)
{
//do some logic to handle null condition
//or simply exit the function if there is no logic to be done :
return;
}
var tag = ((ListPickerItem)OptionSelection.SelectedItem).Tag;
//value of Tag is a string according to your XAML
if (tag.Equals("1"))
{
MessageBox.Show("Item1 selected");
}
}
这里的问题是什么?它给出了一个异常,这里详细介绍了“System.NullReferenceException”类型的异常发生在DataBoundApp3.DLL中,但未在用户代码中处理其他信息:对象引用未设置为对象的实例。似乎问题已解决了一半!好的,它现在给了我一个例外,OptionSelection=Null(您的代码没有编辑),我已经将:if(OptionSelection.SelectedItem==Null)替换为:if(OptionSelection==Null),这对我很有效,谢谢您的帮助!不客气!如果这个答案对你有用,别忘了接受它。更多信息:
private void Picker(object sender, SelectionChangedEventArgs e)
{
if(OptionSelection.SelectedItem == null)
{
//do some logic to handle null condition
//or simply exit the function if there is no logic to be done :
return;
}
var tag = ((ListPickerItem)OptionSelection.SelectedItem).Tag;
//value of Tag is a string according to your XAML
if (tag.Equals("1"))
{
MessageBox.Show("Item1 selected");
}
}