字典&在列表视图中选择项目时出现KeyValuePair错误

时间:2018-05-13 20:14:40

标签: c# xamarin.forms

我是Xamarin和C#的新手。我正在尝试获取我在列表视图中选择的项目的键和值。

这是我获取listview的代码,这项工作正常,因为我得到了一个项目列表:

//search on the list view
private async Task SearchMyFriendAsync(object sender, TextChangedEventArgs e)
{
    string keyword = SearchFriend.Text;
    Dictionary<string, string> words = await usermanager.GetUserList(keyword);
    FriendsListView.ItemsSource =  words;
}

当我选择一个项目时,我收到一个错误;这是代码:

public async void OnSelectedFriendDescript(object obj, ItemTappedEventArgs args)
{
    var FriendName =  (KeyValuePair<string, string>)args.Item;
}

这是错误:

  

未处理的例外:

     

System.NullReferenceException:未将对象引用设置为对象的实例。

以下是截图:

enter image description here

0 个答案:

没有答案