C++ 类型转换为组件 - 保留字符串属性

C++ Type Conversion to component - preserving string properties

我正在构建 Xamarin.Forms 应用程序并正在使用 Monkey.Robotics plug-in. I have a ObservableCollection<IDevice> devices; line, collecting the results from a device scan. I want to present these in a cross platform choice Pop Up, as explained 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!我认为您需要做的是 select 基于名称返回的正确设备对象:

var device = this.devices.Single(device => device.Name == action);