搜索&在C#中选择机制 [英] Search & Select mechanisms in C#

查看:73
本文介绍了搜索&在C#中选择机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中使用Xamarin,构建一个跨平台的BLE应用程序。它基于此示例应用程序使用 Monkey.Robotics 插件。所有示例都使用

I am using Xamarin in C#, to build a cross platform BLE app. It is based on this example app using the Monkey.Robotics plugin. All the examples use a

ObservableCollection<IDevice> devices;

行,用所有扫描结果填充ListView,然后用户可以手动选择,将变量设置为等于该值。即

line, to populate a ListView with all the scanned results which the user can then manually select, setting a variable to equal that value. i.e.

var device = e.SelectedItem as IDevice;

我正在连接到已知类型的设备,因此在选择该设备后,我想自动设置服务和特征变量。 IDevices的结构似乎有点棘手,所以我认为最简单的是枚举服务然后自动选择与我正在寻找的ID相匹配的服务。喜欢这个:



I am connecting to a known type of device, so after that device is selected, I want to then automatically set the Service and Characteristic variables. The structure of `IDevices` seems a bit tricky, so I thought it would be easiest to enumerate the Services and then automatically select the one that matches the ID I am looking for. Like This:

IAdapter adapter;
IDevice device;
IService AppService;

ObservableCollection<IService> services;

    adapter.DeviceConnected += (s, e) => {

        device = e.Device;
        // when services are discovered
        device.ServicesDiscovered += (object se, EventArgs ea) => {
            if (services.Count == 0)
                Device.BeginInvokeOnMainThread (() => {
                    foreach (var service in device.Services) {
                        if (service.ID == 0x2A37.UuidFromPartial ()) {
                            AppService = service as IService;
                        }  else {
                            services.Add (service);
                        }
                    }
                } );
        } ;

        // start looking for services
        device.DiscoverServices ();
    };



这是你怎么做的,或者你会收集所有的结果,然后检查它们?在这种情况下,你将如何构建?



我也可以有一个函数,`SearchFor(x)`,并设置一个无处不在的`ObservableCollection`,然后把x扔进去?不确定它是否可以处理`IService`和`ICharacteristic`,除非我只有2个if语句并为每个语句定义了不同的行为。尽管如此,看到一些关于前进的最佳方式的建议仍然是上帝。



任何想法/建议都会非常感激。谢谢。


Is this how you would do it, or would you collect all the results and then check through them? In which case, how would you structure that?

I could also have a function, to `SearchFor(x)`, and set up a ubiquitous `ObservableCollection`, and just throw x into it? Not sure if it could deal with both `IService` and `ICharacteristic`, unless I just has 2 if statements and defined different behaviour for each. Still, it would be god to see some suggestions for the best way to move forward.

Any thoughts / suggestions would be much appreciated. Thanks.

推荐答案

以这种方式执行此操作可以在找到设备时更新列表。那么,你如何做到这一点取决于你想要达到的目标。在这种情况下,演示应用程序正在扫描并列出设备,因此在这种情况下它是合适的。
Doing it in this manner allows the list to be updated as the devices are found. So, how you do it depends on what you're trying to achieve. In this case the demo app is scanning for and listing devices, so in this case it's appropriate.


这篇关于搜索&amp;在C#中选择机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆