【发布时间】:2021-01-08 20:59:06
【问题描述】:
我想弄清楚这一点正在失去理智。我正在开发一个大型 Windows 应用程序,它要求主窗体中的所有控件几乎实时更新其值。我将这个连续方法的处理移到它自己的线程中,这通常很好,但我知道它需要我创建一个委托来设置我在不同线程中创建的控件。但是,我有一系列按钮,它们需要为每个按钮设置相同的各种属性,但具有不同的值。我在想我可以设置一个 Generic 类型为 Button 的 Delegate,所以当需要更新其属性时,我可以简单地传入正确的按钮控件。但我错过了一些东西,它不起作用:
//If this is wrong, please let me know
private delegate void SafeButtonText<T>(string value) where T : Button;
private void SetButtonTextSafe<T>(string value) where T : Button
{
//Using the generic Button passed in, set its values
if (T.InvokeRequired) //This doesn't compile
{
var d = new SafeButtonText<T>(SetButtonTextSafe<T>);
T.Invoke(d, new object[] { value }); //This doesn't compile
}
else
T.Text = value; //This doesn't compile
}
我以为我可以这样使用它(这似乎不可能)
SetButtonTextSafe<qualityButton>(values[0]);
如果可以,或者有更好的方法,请随时详细告诉我。 (如果我可以在 Button 上使用它,我也会为其他控件类型创建另一个委托)
提前致谢。
【问题讨论】:
-
那是哪个 UI 框架? Winforms?