MessageDialog - needs to wait for user input

早过忘川 提交于 2019-12-05 21:13:34
Dustin Kingen

See Win8 C# Metro dispatcher and RPC_E_WRONG_THREAD and CoreDispatcher.RunAsync.

Since your method isn't executing on the Dispatcher you're going to need to invoke the code on it manually. In order to avoid refactoring into a callback pattern you can use a TaskCompletionSource(T) to set the result and the background thread will continue after the result is set.

var tcs = new TaskCompletionSource<bool>();
var dialogTask = tcs.Task;

MessageDialog md = new MessageDialog(question);
md.Commands.Add(new UICommand("Yes"));
md.Commands.Add(new UICommand("No"));

Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => {
    var result = await md.ShowAsync();
    var canSwitch = result.Label == "Yes";
    tcs.SetResult(canSwitch);
});

var result = await dialogTask;
return result;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!