Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在listview中动态显示设备状态的一种方法_C#_Android_Listview_Xamarin_Bluetooth Lowenergy - Fatal编程技术网

C# 在listview中动态显示设备状态的一种方法

C# 在listview中动态显示设备状态的一种方法,c#,android,listview,xamarin,bluetooth-lowenergy,C#,Android,Listview,Xamarin,Bluetooth Lowenergy,我的问题不是基于崩溃,而是出于好奇,我有一个BLE应用程序和一个我的主页,我扫描设备并在列表视图中显示它们。我需要动态显示设备状态,但StateChange处理程序仅用于适配器。我现在的做法是,每次连接尝试后,我都会显示一个状态刷新的listview,但如果按钮的事件处理程序执行后其中一个设备断开连接,它不会自然更新,因为它不是动态实现的,但是以一种方式循环通过一个可观察的设备集合,然后将每个状态保存到另一个字符串集合。我看到xaml中有一个名为NativeDevice.Name的绑定属性,它可

我的问题不是基于崩溃,而是出于好奇,我有一个BLE应用程序和一个我的主页,我扫描设备并在列表视图中显示它们。我需要动态显示设备状态,但StateChange处理程序仅用于适配器。我现在的做法是,每次连接尝试后,我都会显示一个状态刷新的listview,但如果按钮的事件处理程序执行后其中一个设备断开连接,它不会自然更新,因为它不是动态实现的,但是以一种方式循环通过一个可观察的设备集合,然后将每个状态保存到另一个字符串集合。我看到xaml中有一个名为NativeDevice.Name的绑定属性,它可以工作并在listview中显示设备名称,但NativeDevice.State返回一个枚举,我想它不会在listview中显示。我想知道soemone是否遇到了这个问题,并创建了一种以正确方式更新状态的方法。我是Xamarin的新手,所以如果有人觉得这个问题很烦人,请原谅

private readonly IAdapter _bluetoothAdapter;
        private List<IDevice> _gattDevices = new List<IDevice>();
        private List<IDevice> bledevicesFound = new List<IDevice>();
        CancellationTokenSource source = new CancellationTokenSource();
        ObservableCollection<string> statusDevice= new ObservableCollection<string>();
        public MainPage()
        {
            InitializeComponent();
            _bluetoothAdapter = CrossBluetoothLE.Current.Adapter;
            _bluetoothAdapter.DeviceDiscovered += (s, a) =>
            {
                _gattDevices.Add(a.Device);
            };
            CancellationToken token = source.Token;

        }
 _gattDevices.Clear();
            await _bluetoothAdapter.StartScanningForDevicesAsync();
            foreach (var device in _gattDevices)
            {
                if (device.Name == "BLEDevice")
                {
                    bledevicesFound.Add(device);
                }
            }
            foreach (var item in bledevicesFound)
            {
                statusDevice.Add(item.State.ToString());
            }
            listView.ItemsSource = eggsFound.ToArray();
            statusEggsView.ItemsSource = statusEggs;
专用只读IAdapter\u蓝牙适配器;
私有列表_gattDevices=新列表();
私有列表bledevicesFound=新列表();
CancellationTokenSource=新的CancellationTokenSource();
ObservableCollection Status设备=新的ObservableCollection();
公共主页()
{
初始化组件();
_bluetoothAdapter=CrossBluetoothLE.Current.Adapter;
_bluetoothAdapter.DeviceDiscovered+=(s,a)=>
{
_gattDevices.Add(一种装置);
};
CancellationToken=source.token;
}
_gattDevices.Clear();
等待_bluetoothAdapter.StartScanningForDevicesAsync();
foreach(gattDevices中的var设备)
{
如果(device.Name==“BLEDevice”)
{
bledevicesFound.Add(设备);
}
}
foreach(bledevicesFound中的var项)
{
statusDevice.Add(item.State.ToString());
}
listView.ItemsSource=eggsFound.ToArray();
statusEggsView.ItemsSource=statusEggs;

使用IValueConverter将枚举转换为可以显示的字符串