UWP 在线程里操作UI

如下函数,

        public async Task ShowMessageDialog(string text)
        {
            if (Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
            {
                //操作UI
            }
            else
            {
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => await ShowMessageDialog(text));
            }

        }

例如在线程中显示弹窗的函数如下,

        public async Task ShowMessageDialog(string text)
        {
            if (Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess)
            {
                var msgDialog = new MessageDialog(text) { Title = "Warmming" };
                msgDialog.Commands.Add(new UICommand("OK"));

                await msgDialog.ShowAsync();
            }
            else
            {
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => await ShowMessageDialog(text));
            }

        }

 

 

你可能感兴趣的:(UWP)