我目前正在使用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刘
你是说事件处理程序没有触发,或者你说btn.BackColor的调用没有任何明显的效果? – ctacke
设备在超出范围后再次进入范围后,背景颜色没有明显的效果 –
因此,您在该行上放置了一个断点,并验证该行代码正在执行,是否正确? – ctacke