博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Update UI from an asynchronous thread
阅读量:6712 次
发布时间:2019-06-25

本文共 1439 字,大约阅读时间需要 4 分钟。

One of the most common tasks you need to perform in a Windows Phone application is updating the UI from a separate thread.
 
For example, you may be download some content asynchronously using a WebClient class and when the operation is completed, you want to update the UI with the content that was downloaded. 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. The following code snippet makes this clear:
 
    if (Dispatcher.CheckAccess() == false)
    {
        //---you have to update the UI through the BeginInvoke() method---
        Dispatcher.BeginInvoke(() =>
            txtStatus.Text = "Something happened..."
        );
    }
    else
    {
        //---you can update the UI directly---
        txtStatus.Text = "Something happened..."
    }
 
If you have more than one statement to perform in the 
BeginInvoke() method, you can group them into a method and call it like this:
 
        private void UpdateUI(String text)
        {
            txtStatus.Text = text;
            btnLogin.Content = text;
        }
        ...
        ...
 
            Dispatcher.BeginInvoke(() =>
                UpdateUI("Something happened...")

 

            );

转载于:https://www.cnblogs.com/MinieGoGo/p/3421556.html

你可能感兴趣的文章
从Vue.js源码看nextTick机制
查看>>
前端如何处理emoji表情
查看>>
Git Client 安装及 SSH 公钥配置
查看>>
flutter接入现有的app详细介绍
查看>>
vue的虚拟dom
查看>>
如何设置一个本地测试服务器?
查看>>
iOS11以上 获取系统剩余可用空间不准确
查看>>
警告忽略
查看>>
Python3 CookBook | 迭代器与生成器
查看>>
深入理解 Android 中的各种 Context
查看>>
Android 6 0 运行时权限处理解析
查看>>
JavaScript引用类型之Array类型API详解
查看>>
数据库事务和MVCC多版本并发控制
查看>>
自定义控件实践-倒计时控件
查看>>
《JavaScript高级程序设计(第三版)》
查看>>
随手记 - Springboot Application Properties 值
查看>>
java B2B2C Springcloud多租户电子商城系统- 分布式事务
查看>>
屏幕方向读取与锁定:Screen Orientation API
查看>>
记:解决angular报错'Missing locale data for the locale "zh-cn"
查看>>
【半月刊 2】前端高频面试题及答案汇总
查看>>