簡體   English   中英

Dispatcher.BeginInvoke帶參數的操作

[英]Dispatcher.BeginInvoke Action with Parameters

在處理一些電阻之前,我正在使用Dispatcher來更新我的UI。 問題是BeginInvoke(DispatcherPriorty, 新的ACTION )的一部分是我被困住的地方。 我想參數調用一個方法我不知道為什么。

那是我現在的Dispatcher:

void s_SizeChanged(object sender, SizeChangedEventArgs e)
{
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(test));

}

這是我打電話的方法:

public void test()
{
    foreach (Structures s in ((TreeView)this.cont.Children[0]).Items)
        s.updateRelationLines(this.Data, this.cont.ColumnDefinitions[1]);
}

我只想用參數替換this.Datathis.cont.Columndefinitions[1]

您可以使用lambda表達式:

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, 
    new Action(() => test(param1, param2)));

這基本上創建了一個匿名方法

void myMethod() {
    test(param1, param2);
}

並通過調度程序調用此方法。 一些編譯器魔術確保param1和param2可用於此方法,即使它們僅在s_SizeChanged方法的范圍內。 有關詳細信息,請訪問:

你應該能夠做到:

Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action<Type1, Type2>(test), type1, type2);

回調如下:

public void test(Type1 type1, Type2 type2) {
        foreach (Structures s in ((TreeView)this.cont.Children[0]).Items)
            s.updateRelationLines(type1, type2);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM