我正在创建一个Webapp来控制我家里的一些灯光。我不明白为什么在触发事件无效后图像没有更新;为什么?而如果我通过按钮将其触发,则实际上是在改变。
到目前为止,我已经使用KNX.Net库进行了尝试
...
public void Event(string address, string state)
{
if (address.Equals(CH03LightOnOffAddressStatus) || address.Equals(CH04LightOnOffAddressStatus))
{
System.Diagnostics.Debug.WriteLine("New Event: device " + address + " has status (" + state + ") --> " + _connection.FromDataPoint("9.001", state));
}
else if (
address.Equals(CH01LightOnOffAddressStatus) ||
address.Equals(CH02LightOnOffAddressStatus)
)
{
var data = string.Empty;
if (state.Length == 1)
{
data = ((byte)state[0]).ToString();
}
else
{
var bytes = new byte[state.Length];
for (var i = 0; i < state.Length; i++)
{
bytes[i] = Convert.ToByte(state[i]);
}
data = state.Aggregate(data, (current, t) => current + t.ToString());
}
System.Diagnostics.Debug.WriteLine("New Event: device " + address + " has status --> " + data);
condition = data;
nowAddress = address;
}
if (condition == "1")
{
Image1.ImageUrl = @"\Res\Call.png";
}
else
{
Image1.ImageUrl = @"\Res\Bomb.png";
}
}
...
当我像这样射击时,图像确实在改变
...
private void CH01LightOn()
{
_connection.Action(CH01LightOnOffAddress, true);
Thread.Sleep(500);
if (condition == "1")
{
Image1.ImageUrl = @"\Res\Call.png";
}
else
{
Image1.ImageUrl = @"\Res\Bomb.png";
}
}
...
事件发生后,我只需要更改图像即可。 预先谢谢你。
答案 0 :(得分:0)
这可能是线程问题。如果未在UI线程上调用Event方法,则无法直接访问WinForms控件,而必须使用Control.Invoke
。
要尝试此操作,请将Image1.ImageUrl = @"\Res\Call.png"
替换为:
if (Image1.InvokeRequired)
{
Image1.Invoke((Action)(() => Image1.ImageUrl = @"\Res\Call.png"));
}
else
{
Image1.ImageUrl = @"\Res\Call.png"
}