wpf 如何使用 Dispatcher.Invoke 返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2491927/
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-08 21:36:13  来源:igfitidea点击:

How to return a value with Dispatcher.Invoke?

wpfreturninvokedispatcher

提问by toni

Anyone knows how to return a value from Dispatcher.Invokein wpf? I want to return the selected index for a ComboBox.

任何人都知道如何从Dispatcher. Invokewpf 中?我想为ComboBox返回选定的索引。

Thanks!

谢谢!

回答by user216652

There's another way that returns value from Invoke():

还有另一种从 Invoke() 返回值的方法:

object oIsLoaded = container.Dispatcher.Invoke( new Func<bool> ( () =>
    {
        return container.IsLoaded;
    })
);

And by the way, chances are that the initial code (which is working with delegate) won't modify oIsLoadedat all; So I'd rather use a Func<>for returning a value from that kind of function.

顺便说一句,很可能初始代码(与委托一起工作)根本不会修改oIsLoaded;所以我宁愿使用 aFunc<>从那种函数返回一个值。

回答by toni

int result = -1;

// this is synchronous
myCombo.Invoke(() => 
{
  result = myCombo.SelectedIndex;
});

return result;

This is, of course, kind of clunky. Better design would be to implement INotifyPropertyChanged in your VM, create a SelectedIndex property and bind the SelectedIndexproperty 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.

当然,这有点笨拙。更好的设计是在 VM 中实现 INotifyPropertyChanged,创建 SelectedIndex 属性并将SelectedIndex组合框的属性绑定到它。INPC 绑定对线程不敏感(3.5 或 4.0+,我不记得是哪个),因此您可以从 VM 中的不同线程读取和更新这些属性而无需担心。

回答by toni

This is my method to retrieve selected value for a combobox, how can I say delegate to return value?

这是我为组合框检索选定值的方法,我怎么能说委托返回值?

    private object getValueCB(System.Windows.Controls.ComboBox cb)
    {
        object obj;


            if (!cb.Dispatcher.CheckAccess())
            {
                obj = cb.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  new Action(
                    delegate()
                    {
                        obj = cb.SelectedValue;
                    }
                ));

                return obj;
            }
            else
            {
                return obj = cb.SelectedValue;
            }

    }

回答by toni

I have solved this. The solution is create a custom delegate that returns the desired type like this:

我已经解决了这个问题。解决方案是创建一个自定义委托,返回所需的类型,如下所示:

    private object GetValueCB(System.Windows.Controls.ComboBox cb)
    {
        object obj = null;


            if (!cb.Dispatcher.CheckAccess())
            {
                obj = cb.Dispatcher.Invoke(
                  System.Windows.Threading.DispatcherPriority.Normal,
                  (MyDelegate)
                    delegate()
                    {
                        return (obj = cb.SelectedValue);
                    }
                );

                return obj;
            }
            else
            {
                return obj = cb.SelectedValue;
            }

    }

    public delegate object MyDelegate();

回答by Chris

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.

Dispatcher.Invoke() 实际上从您调用的委托返回返回值,因此相应地更改您的委托。

Return Value

Type: System.Object The return value from the delegate being invoked or null if the delegate has no return value.

返回值

类型:System.Object 正在调用的委托的返回值,如果委托没有返回值,则为 null。

Source

来源