wpf WPF中的MethodInvoker?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16188287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:40:02  来源:igfitidea点击:

MethodInvoker in WPF?

.netwpfwinforms

提问by Brij

Is there any in-built delegate like System.Windows.Forms.MethodInvokerfor WPF?

有没有像System.Windows.Forms.MethodInvokerfor那样的内置委托WPF

I was porting some functionality from WinFormsto WPF, for this I was not wising to import winforms reference, is there any corresponding delegate in WPF?

我正在从WinFormsto移植一些功能WPF,为此我不知道导入 winforms 参考,是否有任何相应的委托WPF

采纳答案by Alex

MethodInvokeris used in WPF also. And you can use it in Dispatcher.Invoke or Control.Invoke methods. Also you can use Actiondelegate which has many overridings. They both will be efficient.

MethodInvoker也用于 WPF。您可以在 Dispatcher.Invoke 或 Control.Invoke 方法中使用它。您也可以使用具有许多覆盖的Action委托。他们都会有效率。

回答by Mehmet Ince

You can use just like this;

你可以这样使用;

Dispatcher.BeginInvoke(new Action(delegate
  {
    // Do your work
  }));

回答by Jian Zhong

For example, you have button control btnLogin you can use it like this:

例如,您有按钮控件 btnLogin,您可以像这样使用它:

In WPF:

在 WPF 中:

btnLogin.Dispatcher.Invoke(new Action(delegate
{
    // Running on the UI thread
    btnLogin.Content = "TEST";
}));

In Winform:

在 Winform 中:

btnLogin.Invoke((MethodInvoker)delegate {
    // Running on the UI thread
    btnLogin.Text = "TEST";
});

Cheers!

干杯!