简体   繁体   中英

Windows phone UI update from async function

I am trying to update the view model in windows phone app. But on updating the view model the UI is not getting updated.

        var response = await request.GetResponseAsync();

        Stream source = response.GetResponseStream();

        this.viewModel.VideoDownloadPercentage = 50;

Though the below code is able to update the UI

       var response = await request.GetResponseAsync();

       this.viewModel.VideoDownloadPercentage = 50;

       Stream source = response.GetResponseStream();

Can anyone provide pointers on why putting the viewModel before GetResponseAsync updates the UI but putting it after doesn't?

Updating the UI directly from an asynchronous thread is not allowed, as UI controls are not thread-safe.

The easiest way to update the UI from an asynchronous thread is to use the Dispatcher class.

To determine if you can update an UI directly, you can use the CheckAccess() method. If this method returns a true, it means you can directly update the UI. Else, you have to use the BeginInvoke() method of the Dispatcher class to update the UI in a thread-safe manner.

original post : Windows phone UI update from async function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM