开发者

How can I update UI when Observable.Context is not available?

I have a WinFrom App, use synchronous method to download string from a url, and use Rx ToAsync Method to make it asynchronous and get the observable result, and when the result comes I show it on the Form.

Yesterday, I updated Rx to the latest release, and it was told that "Observable does not contain a def开发者_如何学Cinition of Context". I tried comment this line, the codes threw an exception that "Cross-thread operation not valid: Control 'tbx_Reference' accessed from a thread other than the thread it was created on."

I want to show the asynchronous result using Subscribe method. How can I fix this problem? thanks very much.

public static IObservable<TResult> DoWorkAsync(TParameter parameter,
        Func<TParameter,TResult> actionSync)
    {
        Observable.Context = SynchronizationContext.Current;

        Func<TParameter, IObservable<TResult>> ActionAsync =  actionSync.ToAsync();

        IObservable<TResult> results = from result in ActionAsync(parameter)
                                       select result;

        return results;
    }


For your return statement, try:

return results.ObserveOnDispatcher();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜