从var项中获取值?

时间:2014-04-24 04:15:39

标签: c# windows-phone-8

我有wp8应用程序,列表选择器绑定到xml数据源,当我选择项目我希望能够获得“源”src位置文件位置我有这个功能当我检查项目我发现src选择了名称enter image description here

private void SoudListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    // TODO: Add event handler implementation here.

    var item  = (sender as ListPicker).SelectedItem;
    MessageBox.Show("Selected Item is : " + item);
}

1 个答案:

答案 0 :(得分:3)

var使用赋值语句右侧的结果类型来推断它的类型,在这种情况下(我相信)是object

要使属性可见,您需要将SelectedItem强制转换为实际类型。

private void SoudListPicker_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    // TODO: Add event handler implementation here.

    var item  = ((sender as ListPicker).SelectedItem) as Expression.Blend.SampleData.soundsEFXSampleDataSource3.sound;

    if(item != null)
        MessageBox.Show("Selected Item is : " + item.src);
}