【问题标题】:Invalid cast when getting object from ListView从 ListView 获取对象时强制转换无效
【发布时间】:2018-08-12 19:57:01
【问题描述】:

所以我有一个 ListView,其中填充了来自 ObservableCollection 的 IDeviceIDevice基本上是外围的蓝牙设备。 IE。一个对象。

当用户在 ListView 中按下给定设备时,我最终希望它调用我的 Connect() 方法。 - 但现在,它只是在我测试时被注释掉。

listView.ItemTapped += async (s, e) => 
{
    var answer = await DisplayAlert("Tapped", "Connect to device?", "Yes", "No");
    if(answer == true)
    {
        IDevice name = (IDevice)e;
        await DisplayAlert("name of BLE", name.Name + " " + name.State.ToString(), "OK");
        //Connect((IDevice)e);
    }
};

问题是,当我进入 if 语句时,我正在做一个无效的转换。即(IDevice)e

我似乎无法理解我在这里做错了什么。

【问题讨论】:

    标签: listview xamarin xamarin.forms


    【解决方案1】:

    eItemTappedEventArgs 类型,其中包含一个属性Item

    IDevice name = (IDevice)e.Item;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      相关资源
      最近更新 更多