如何使用 Dispatcher.Invoke 返回值?
任何人都知道如何从 Dispatcher< 返回值/code>
.
在
wpf 中调用
?我想返回 ComboBox。
谢谢!
Anyone knows how to return a value from Dispatcher
.Invoke
in wpf? I want to return the selected index for a ComboBox.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
还有另一种从 Invoke() 返回值的方法:
顺便说一句,初始代码(与委托一起使用)很可能根本不会修改
oIsLoaded
;所以我宁愿使用Func
< /a> 用于从此类函数返回值。There's another way that returns value from Invoke():
And by the way, chances are that the initial code (which is working with delegate) won't modify
oIsLoaded
at all; So I'd rather use aFunc<>
for returning a value from that kind of function.当然,这有点笨拙。更好的设计是在虚拟机中实现 INotifyPropertyChanged,创建 SelectedIndex 属性并将组合框的
SelectedIndex
属性绑定到它。 INPC 绑定是线程不敏感的(3.5 或 4.0+,我不记得是哪一个),因此您可以放心地从 VM 中的不同线程读取和更新这些属性。This is, of course, kind of clunky. Better design would be to implement INotifyPropertyChanged in your VM, create a SelectedIndex property and bind the
SelectedIndex
property of your combo box to it. INPC binds are thread-insensitive (3.5 or 4.0+, I don't remember which), so you can read and update these properties from different threads in your VM without worry.这是我检索组合框选定值的方法,我怎么能说委托返回值呢?
This is my method to retrieve selected value for a combobox, how can I say delegate to return value?
你不能直接这样做,但你可以这样做。
Dispatcher.Invoke() 实际上从您调用的委托返回返回值,因此请相应地更改您的委托。
来源
You can't do this directly but you can do this.
Dispatcher.Invoke() actually returns the return value from the delegate you call, so alter your delegate accordingly.
Source
我已经解决了这个问题。解决方案是创建一个自定义委托,它返回所需的类型,如下所示:
I have solved this. The solution is create a custom delegate that returns the desired type like this: