通用类型:不存在从 ToolStripStatusLabel 到 Control 的隐式引用转换
我想从 SerialPort DataReceived 事件处理程序更新 UI。我发现了一个问题,因为事件处理程序隐式地运行在与表单不同的线程中,所以不是简单地更新 UI......
myLabel.Text = "Some text";
我必须采取以下方法:
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);
}
}
到目前为止一切顺利...但是,现在我想更新 ToolStripStatusLabel - 使用相同的方法会产生“ToolStripStatusLabel 和 Forms.Control 之间没有隐式引用转换”错误。
据我所知,问题源于您无法调用 ToolStripStatusLabel。
那么我该如何最好地处理这个问题呢?
注意:代表等处于我当前能力的阈值,因此如果能提供解释和解决方案,我们将不胜感激。
更新1:为了澄清,我尝试创建与InvokeControlAction等效的ToolStripStatusLabel,但这不起作用,因为它没有调用方法。
结果:重新审视我的解决方案后,我按照吉米最初的建议将其实现为扩展方法。
我创建了一个静态 ExtensionMethod 类(在它自己的“ExtensionMethods”命名空间中),添加到 InvokeOnToolStripItem 方法中,添加“using ExtensionMethods;”我原来的类中的指令并调用方法如下:
tsStatusValue.InvokeOnToolStripItem(ts => ts.Text = "ALARM signal received");
I'm wanting to update the UI from a SerialPort DataReceived event handler. 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...
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.
From what I have read, the problems stems from the fact that you can't Invoke a 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.
RESULT: After revisiting my solution, I've implemented it as an Extension Method as Jimmy originally suggested.
I created an static ExtensionMethod class (in it's own 'ExtensionMethods' namespace), added in the InvokeOnToolStripItem method, add a 'using ExtensionMethods;' directive in my original class and called the methods as follows:
tsStatusValue.InvokeOnToolStripItem(ts => ts.Text = "ALARM signal received");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ToolStripStatusLabel
确实不继承自Control
,这就是您的通用约束因您发布的确切原因而失败的原因。更重要的是,
ToolStripStatusLabel
(或实际上任何ToolStripItem
)没有Invoke
方法。幸运的是,包含ToolStrip
,可以使用GetCurrentParent
方法。以下是适用于任何
ToolStripItem
的扩展方法:您只需调用即可使用它:
ToolStripStatusLabel
does not inherit fromControl
, and that's why your generic constraint fails for the exact reason you posted.What is more,
ToolStripStatusLabel
(or anyToolStripItem
in fact) doesn't haveInvoke
method. Luckily, containingToolStrip
has, which can be easily accessed usingGetCurrentParent
method.Here's extension method that works on any
ToolStripItem
:You can use it by simply calling:
为了解释错误消息,您已编写
但 ToolStripStatusLabel 不继承自 Control。
不确定这是否对您有帮助,而且还没有真正的解决方案:(
To explain the error message, you have writen
but ToolStripStatusLabel does not inherit from Control.
Not sure if that helps you at all and don't really have a solution yet :(