简体   繁体   English

通用类型:没有从ToolStripStatusLabel到Control的隐式引用转换

[英]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.

相关问题 用户定义类型从引用类型到接口的隐式引用转换 - Implicit Reference Conversion from reference type to interface for user defined types 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A” 没有来自……通用查询处理程序cqrs的隐式引用转换。 - There is no implicit reference conversion from… generic query handler cqrs 具有泛型类的泛型函数:无隐式引用转换 - Generic function with generic class: no implicit reference conversion 使用引用类型和值类型参数化的泛型类型的隐式转换 - Implicit convertion of generic types parameterized with reference types vs value types 通用存储库和通用DbContext中没有隐式引用转换错误 - no implicit reference conversion error in a generic repository and generic DbContext “System.Collections.Generic.IList”没有隐式引用转换 <double> &#39;to&#39;System.Collections.IList&#39; - There is no implicit reference conversion from 'System.Collections.Generic.IList<double>' to 'System.Collections.IList' &#39;System.Collections.Generic.List没有隐式引用转换 <T> &#39;到&#39;T&#39; - There is no implicit reference conversion from 'System.Collections.Generic.List<T>' to 'T' 类型 &#39;&#39; 不能用作泛型类型或方法 &#39;&#39; 中的类型参数 &#39;T&#39;。 没有从 &#39;&#39; 到 &#39;&#39; 的隐式引用转换 - The type '' cannot be used as type parameter 'T' in the generic type or method ''. There is no implicit reference conversion from '' to '' 该类型不能在泛型类型或方法中用作类型“T”。 没有隐式的引用转换 - The type cannot be used as type 'T' in the generic type or method. There is no implicit reference conversion from to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM