我正在构建一个Xamarin.Forms应用程序并使用Monkey.Robotics插件。我有一条ObservableCollection<IDevice> devices;
行,收集设备扫描的结果。我想在跨平台选择Pop Up中呈现这些,正如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输入保持该格式?
答案 0 :(得分:1)
由于string
未实现IDevice
,您无法将String
转换为IDevice
的实现!我认为您需要做的是根据返回的名称选择正确的设备对象:
var device = this.devices.Single(device => device.Name == action);