Winforms 到 WPF 的转换:BeginInvoke 到什么?
这是我来自 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 友好的? 到目前为止,我已经:
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 代码到处都有第一个实现,所以我真的很想避免必须对所有代表进行去匿名化。
编辑:我可以尝试像以前一样使用 MethodInvoker,但编译器会感到困惑。 MethodInvoker 是 System.Windows.Forms 的一部分,因此使用该方法不起作用。 例如:
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 的使用是不合规矩的。 是否有单独的实现,或者使用相同行为的其他方式?
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");
}
}
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");
});
}
}
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.
Edit: 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");
}));
}
}
That use of MethodInvoker is not kosher. Is there a separate implementation of it, or some other way to use the same behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
System.Windows.Forms.MethodInvoker 只是一个不带参数并返回 void 的委托。 在 WPF 中,您只需将其替换为 System.Action 即可。 还有其他内置委托接受参数,返回值,或两者。
在你的情况下,
变成
System.Windows.Forms.MethodInvoker is simply a Delegate that take no parameters and returns void. In WPF, you can just replace it with System.Action. There are also other built-in Delegates that accept parameters, return values, or both.
In your case,
becomes
我认为您需要更改委托的签名:
另外,使用BackgroundWorker 组件进行查找。 不仅适用于 wpf,还适用于 winform 异步操作。
I think you need to change the signature of the delegate:
Also, look up using the BackgroundWorker component. Not just for wpf but also for the winform async operations.