简体   繁体   中英

MethodInvoker in WPF?

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

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

You can use just like this;

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

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

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

In WPF:

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

In Winform:

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

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM