[英]Generic Types: There is no implicit reference conversion from ToolStripStatusLabel to Control
I'm wanting to update the UI from a SerialPort DataReceived event handler. 我想从SerialPort DataReceived事件处理程序更新UI。 I discovered a problem because the event handler was implicitly running in a different thread to the form, so rather than simply update the UI...
我发现了一个问题,因为事件处理程序隐式运行在与表单不同的线程中,所以不是简单地更新UI ...
myLabel.Text = "Some text";
...I had to take the following approach: ......我不得不采取以下方法:
InvokeControlAction<Label>(myLabel, lbl=> lbl.Text= "Some text");
...
public static void InvokeControlAction<t>(t cont, Action<t> action) where t : Control
{
if (cont.InvokeRequired)
{
cont.Invoke(new Action<t, Action<t>>(InvokeControlAction),
new object[] { cont, action });
}
else
{
action(cont);
}
}
So far so good... However, now I want to update a ToolStripStatusLabel - using the same approach yields a 'there is no implicit reference conversion between ToolStripStatusLabel and Forms.Control' error. 到目前为止一切都那么好......但是,现在我想更新ToolStripStatusLabel - 使用相同的方法产生'ToolStripStatusLabel和Forms.Control之间没有隐式引用转换'错误。
From what I have read, the problems stems from the fact that you can't Invoke a ToolStripStatusLabel. 从我所读到的,问题源于你不能调用ToolStripStatusLabel的事实。
So how best do I handle this? 那么我该如何处理呢?
Note: delegates, etc are at the threshold of my current ability, so an explanation alongside a solution would be appreciated. 注意:代表等处于我当前能力的门槛,因此可以理解解决方案和解决方案。
UPDATE 1: Just to clarify, I tried to create ToolStripStatusLabel equivalent of InvokeControlAction, but this won't work because it doesn't have an invoke method. 更新1:为了澄清,我试图创建相当于InvokeControlAction的ToolStripStatusLabel,但这不起作用,因为它没有调用方法。
RESULT : After revisiting my solution, I've implemented it as an Extension Method as Jimmy originally suggested. 结果 :在重新访问我的解决方案后,我已将其实现为Jimmy最初建议的扩展方法。
I created an static ExtensionMethod class (in it's own 'ExtensionMethods' namespace), added in the InvokeOnToolStripItem method, add a 'using ExtensionMethods;' 我创建了一个静态的ExtensionMethod类(在它自己的'ExtensionMethods'命名空间中),在InvokeOnToolStripItem方法中添加了一个'using ExtensionMethods;' directive in my original class and called the methods as follows:
我的原始类中的指令,并调用如下方法:
tsStatusValue.InvokeOnToolStripItem(ts => ts.Text = "ALARM signal received");
ToolStripStatusLabel
does not inherit from Control
, and that's why your generic constraint fails for the exact reason you posted. ToolStripStatusLabel
不会从Control
继承,这就是您的通用约束因您发布的确切原因而失败的原因。
What is more, ToolStripStatusLabel
(or any ToolStripItem
in fact) doesn't have Invoke
method. 更重要的是,
ToolStripStatusLabel
(或任何ToolStripItem
实际上)没有Invoke
方法。 Luckily, containing ToolStrip
has, which can be easily accessed using GetCurrentParent
method. 幸运的是,包含
ToolStrip
,可以使用GetCurrentParent
方法轻松访问。
Here's extension method that works on any ToolStripItem
: 这是适用于任何
ToolStripItem
的扩展方法:
public static void InvokeOnToolStripItem<T>(this T item, Action<T> action)
where T : ToolStripItem
{
ToolStrip parent = item.GetCurrentParent();
if (parent.InvokeRequired)
{
parent.Invoke((Delegate)action, new object[] { item });
}
else
{
action(item);
}
}
You can use it by simply calling: 你可以通过简单地调用它来使用它:
myToolStripLabel.InvokeOnToolStripItem(label => label.Text = "Updated!");
myToolStripProgressBar.InvokeOnToolStripItem(bar => bar.PerformStep());
To explain the error message, you have writen 要解释错误消息,您已经写了
where t : Control
but ToolStripStatusLabel does not inherit from Control. 但ToolStripStatusLabel不从Control继承。
Not sure if that helps you at all and don't really have a solution yet :( 不确定这对你有什么帮助,还没有真正的解决方案:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.