0

我目前正在开发一个 windows mobile 6.5 应用程序。该应用程序具有蓝牙功能,可从串行端口读取数据。读取数据后,事件处理程序会将数据从较低级别的类传递到 UI 视图。UI 将根据从串行端口读取的值更新值并更改按钮的背景颜色。但是,有时,蓝牙设备可能超出范围。换言之,手持装置无法接收来自远端蓝牙装置的任何数据。

当设备在范围内且手持设备可以从串口接收数据时,接收到的值和按钮的背景颜色将根据接收到的值在 UI 中相应改变。但是,在设备超出范围后再次进入范围后,事件处理程序由于某种原因不起作用,因此,只有值发生变化,按钮的背景颜色无法通过事件处理程序更改。

如果我单击该按钮,颜色将再次改变。看起来只有当焦点在按钮上时颜色才会改变。我可以确认与事件处理程序有关的问题,因为我在标签上打印了 deviceName,事件处理程序不会触发。谁能知道它为什么会发生?或任何更好的想法。

这是我用来保持监听按钮的背景颜色变化及其事件处理程序的代码:

 Button[] gauges = new Button[MonitoringGauges.Count()]; // declare the button variable

    // Create a button for each gauge
    .....       
    .....
    .....

    // Constructor

            for(int k = MonitoringGauges.count -1 ; k >=0 ; k--){  // keep listening to the backcolor change for the button corresponding to the gauge
              if(MonitoringGauges[k] !=null){
                   MonitoringGauges[k].TrainingZoneChanged += new Gauge.TrainingZoneChangedEventHandler(x_TrainingZoneChanged);
               }
            }

        // event handler

        void x_TrainingZoneChanged(string deviceName, string macAddress, Color color){
           if(!string.IsNullOrEmpty(deviceName) && !string.IsNullOrEmpty(macAddress) && color !=null){
             Button btn = gauges.Where(x =>x.Name.equals(deviceName)).First(); // find the correct button for updating the backcolor of the button

        if(btn !=null){
            btn.Invoke((Action) delegate
             {
                 if(color == Color.Black){
                        btn.BackColor = Color.LightBlue;
                 } else{
                        btn.BackColor = color;
                  }

             });
        }
        }
        }

感谢您的帮助。

问候,

SW刘

4

1 回答 1

0

您是否已经尝试过简单的 btn.Refresh() 来强制重绘?可能后跟一个 Application.DoEvents();

于 2012-06-29T09:45:52.980 回答