Winforms到WPF的转换:BeginInvoke要做什么? [英] Winforms to WPF conversion: BeginInvoke to what?

查看:120
本文介绍了Winforms到WPF的转换:BeginInvoke要做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我来自WinForms的旧代码:

Here's my old code from WinForms:

    private void ValueChanged(double inValue1, double inValue2) {
        //only manual mode for this driver, so that's easy.
        if (ValueLabel.InvokeRequired) {
            ValueLabel.Invoke(new MethodInvoker(delegate {
                ValueLabel.Text = (inValue1* inValue2/ 1000).ToString("f1");
            }
                ));
        }
        else {
            ValueLabel.Text = (inValue1* inValue2/ 1000).ToString("f1");
        }
    }

有没有简单的方法可以将其转换为WPF友好?到目前为止,我有:

Is there an easy way to convert this to be WPF friendly? So far, I have:

   private void KVPValueChanged(double inValue1, double inValue2) {
        if (ValueLabel.Dispatcher.Thread == Thread.CurrentThread){
            ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
        } else {
            ValueLabel.Dispatcher.BeginInvoke(delegate {
                ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
            });
        }
    }

但是第二次代表通话失败。如何调用此委托?我想我可以遍历整个委托方法,创建委托方法的实例,调用特定实例等,但是我认为这些匿名委托的全部目的是避免麻烦。另外,我以前的winforms代码在所有地方都有第一个实现,因此我真的想避免不必对所有代表取消匿名。

But that second 'delegate' call fails. How can I invoke this delegate? I suppose I can go through the whole making a delegate method, making an instance of the delegate method, invoking that particular instance, etc, but I thought the whole point of these anonymous delegates was to avoid that hassle. Plus, my old winforms code has that first implementation all over the place, so I'd really like to avoid having to de-anonymize all my delegates.

编辑:我可以像以前一样尝试使用MethodInvoker,但随后编译器会感到困惑。 MethodInvoker是System.Windows.Forms的一部分,因此使用该方法无效。像这样:

I can try to use the MethodInvoker like I was before, but then the compiler gets confused. MethodInvoker is part of System.Windows.Forms, so using that approach doesn't work. As in:

    private void ValueChanged(double inValue1, double inValue2) {
        if (ValueLabel.Dispatcher.Thread == Thread.CurrentThread) {
            ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
        }
        else {
            ValueLabel.Dispatcher.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate {
                ValueLabel.Content = (inValue1* inValue2/ 1000).ToString("f1");
            }));
        }
    }

MethodInvoker的使用不符合要求。

That use of MethodInvoker is not kosher. Is there a separate implementation of it, or some other way to use the same behavior?

推荐答案

我认为您需要更改它的单独实现吗?代表的签名:

I think you need to change the signature of the delegate:

ValueLabel.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate(invalue1, invalue2){
    ValueLabel.Content = ...

此外,还可以使用BackgroundWorker组件进行查找。用于wpf,也用于winform异步操作。

Also, look up using the BackgroundWorker component. Not just for wpf but also for the winform async operations.

这篇关于Winforms到WPF的转换:BeginInvoke要做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆