【wpf】【课设】运用DispatcherTimer自动更新listview信息,选中listview信息链接蓝牙

来源:互联网 发布:成都华育国际网络推广 编辑:程序博客网 时间:2024/05/17 04:39

这个星期主要在研究如何选中listview的信息然后使其与蓝牙连接,周末看了下操作系统,简单的了解了进程和线程,并且把msdn上的Dispatcher类的方法看了个七七八八。

        之前遇到的问题在于从listView_SelectionChanged方法中得到的选中的e.AddedItems[0]是个Anonymous Type。



     Anonymous Type也就是匿名类,无法进行调用,所以卡这里卡了一两天QwQ

     后来发现可以机智的通过选中第几个来获取address(但是总觉得这个有弊端 有安全风险)

private void connect_Click(object sender, RoutedEventArgs e)        {            int a = listView.Items.Count;                            //获取listview那一栏被选中            mchoice = devices[a - 1].DeviceAddress;                  //取到被选中那一栏的地址            Guid mGUID = BluetoothService.ObexObjectPush;            //得到本机guid            cli.Connect(mchoice, mGUID);                             //链接蓝牙            link.Text = "已连接";        }

      因为想做listview自动更新,所以设计多线程操作,便去看了DispatcherTimer。msdn地址:点击打开链接

          

      主要方法有:



   所以可以这样写

timer = new DispatcherTimer();                                                 timer.Interval = TimeSpan.FromSeconds(60);            timer.Tick += timer_Tick;            timer.Start();                                          //开始进程,设置为60s刷新一次


void timer_Tick(object sender, EventArgs e)                          {            ObservableObj.Clear();            devices = cli.DiscoverDevices();            foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo device in devices) //设备搜寻                       {                device.Update();                device.Refresh();                int i;                for (i = 0; i < devices.Length; i++)                {                    ObservableObj.Add(new { Name = devices[i].DeviceName, Address = devices[i].DeviceAddress, Statu = devices[i].Connected });                }                break;            }        }


        就可以实现60s刷新一次listview里面的信息;

   项目现在已经放在github上了,所以就不在这里贴代码了,地址:项目地址




0 0
原创粉丝点击