我正在构建 Xamarin.Forms 应用程序并使用 Monkey.Robotics插入。我有一个 ObservableCollection<IDevice> devices;
行,收集设备扫描的结果。我想在跨平台选择弹出窗口中显示这些,如解释的那样 here .下面的代码是我目前所在的位置,但我遇到了类型转换问题。
var action = await DisplayActionSheet ("ActionSheet: Choose Your Device", "Can't see It?", null, this.devices.Select(device => device.Name).ToArray());
if (action == "Can't see It?"){
//show help prompt
};
var device = action as IDevice; //pass this as device to the service routines...
我在最后一行遇到错误,无法通过内置转换将类型“String”转换为“Robotics.Mobile.Core.Bluetooth.LE.IDevice”有什么办法可以仅在输出上手动执行该转换,同时仍让 DisplayAction 输入保持该格式?
最佳答案
您不能期望将 string
转换为 IDevice
的实现,因为 String
没有实现 IDevice
!我认为您需要做的是根据返回的名称选择正确的设备对象:
var device = this.devices.Single(device => device.Name == action);
关于c# - C++ 类型转换为组件 - 保留字符串属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338881/