似乎每個人都注意到CF無法控制。調用EventHandler類型以外的委託。我發現了幾個方法來解決這個問題,並通過棘手的事件和屬性來捕捉參數,但它僅適用於某些對象/原語。CF中的Control.Invoke委託3.5
問題是當我嘗試發送一個控件時,程序在執行時拋出一個異常:「Control.Invoke必須用於與單獨線程上創建的控件進行交互」無論我檢查Control.IsInvokeRequired和由handler跟蹤槽Invoke方法......到底是什麼,還有就是snipet:
static class Program
{
[MTAThread]
static void Main()
{
Form1 frm = new Form1();
frm.modifier = new CModifier(ref frm);
Application.Run(frm);
}
}
class CModifier
{
internal event EventHandler Modify;
internal CModifier(ref Form1 _frm)
{ frm = _frm; }
private Form1 frm;
internal void Start()
{
Thread t = new Thread(new ThreadStart(TestMe));
t.Start();
}
private void TestMe()
{
frm._current = new Label() { Text = "Try Me" };
Modify(this, EventArgs.Empty);
}
}
public partial class Form1 : Form
{
internal Form1()
{
InitializeComponent();
}
internal CModifier modifier;
internal Control _current;
private void Form1_Load(object sender, EventArgs e)
{
modifier.Modify += new EventHandler(modifier_Modify);
modifier.Start();
}
void modifier_Modify(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(modifier_Modify));
}
this.panel1.Controls.Add(_current);
}
}
我試圖幾乎每一個可能的方式做,我總是得到相同的結果。
我在做什麼錯? ---->有一個「回報」;在調用後丟失。
不管怎麼說,在我的應用程序TE實際誤差(以上只是一個例子),在此過程中:
internal void ManageControls(RSSDecisionMaking.InterfaceBehaviour _behaviour, Control _control)
{
if (this.InvokeRequired)
{
this.Invoke((Action)delegate { ManageControls(_behaviour, _control); });
return;
}
switch (_behaviour)
{
case RSSDecisionMaking.InterfaceBehaviour.Display:
this.pnDisplayContainer.Controls.Clear();
this.pnDisplayContainer.Controls.Add(_control);
break;
case RSSDecisionMaking.InterfaceBehaviour.Command:
this.pnDisplayContainer.Controls.Clear();
this.pnDisplayContainer.Controls.Add(_control);
this.pnDisplayContainer.Controls[0].Focus();
break;
}
}
的主要區別是在調用的東西,我要送從另一個線程控制。
可以告訴我它有什麼不同嗎?爲什麼這不起作用?
哦,夥計!我甚至意識到,你是對的。 –