我有实例化的同步方法事件的视图模型。该事件信号的UI,我们继续之前,需要一个是或否从用户的答案。
I have a ViewModel which instantiates an event in a synchronous method. The event signals to the UI that we need a "Yes" or "No" answer from the user before continuing.
我想显示 MessageDialog 并等待用户提供了一个答案 - 是或否。我有困难的时候这样做。目前,我得到一个 UnauthorizedAccessException 试图做到这一点的时候。
I am trying to display a MessageDialog and wait until the user provides an answer - Yes or No. I am having a difficult time doing this. I currently get an UnauthorizedAccessException when trying to do this.
下面就一起来看看UI中的code:
Here's a look at the code in the UI:
async Task<bool> Instance_SwitchConfirmation(string question) { MessageDialog md = new MessageDialog(question); md.Commands.Add(new UICommand("Yes", CommandInvokedHandler)); md.Commands.Add(new UICommand("No", CommandInvokedHandler)); md.ShowAsync(); return this.canSwitch; } async void CommandInvokedHandler(IUICommand command) { this.canSwitch = command.Label == "Yes" ? true : false; }我曾尝试:
var uiContext = TaskScheduler.FromCurrentSynchronizationContext(); Task.Factory.StartNew(() => { MessageDialog md = new MessageDialog(question); md.Commands.Add(new UICommand("Yes", CommandInvokedHandler)); md.Commands.Add(new UICommand("No", CommandInvokedHandler)); md.ShowAsync(); }, new System.Threading.CancellationToken(), TaskCreationOptions.PreferFairness, uiContext);但这种失败,出现相同的异常。
but this fails with the same exception.
最后,如果我只是等待MessageDialog像这样,不显示对话框,并在UI线程锁定。
Lastly, if I simply await the MessageDialog like so, the dialog is not displayed and the UI thread locks up.
MessageDialog md = new MessageDialog(question); md.Commands.Add(new UICommand("Yes", CommandInvokedHandler)); md.Commands.Add(new UICommand("No", CommandInvokedHandler)); await md.ShowAsync();如果 MessageDialog 不得不的同步版本放映()我会被罚款,但的asynchrnous behvaior MessageDialog 再加上我synchrounous套路,加上跨线程是混淆了我。我想知道,如果有人可以勾勒出什么,我需要做的等待在 MessageDialog 之前,我的后端视图模型继续同步方法。
If MessageDialog had a synchronous version of Show() I would be fine but the asynchrnous behvaior of MessageDialog coupled with my synchrounous routine, coupled with cross threading is confusing me. I'm wondering if someone can outline what I need to do to wait for user input on a MessageDialog before continuing a synchronous method in my backend ViewModel.
在此先感谢您的帮助。
推荐答案请参阅Win8 C#地铁调度员和RPC_E_WRONG_THREAD 和CoreDispatcher.RunAsync.
由于你的方法是不是在调度执行你将需要手动调用code就可以了。为了避免重构为一个回调格局可以使用TaskCompletionSource(T)设置的结果,其结果被设定后后台线程将继续
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;更多推荐
MessageDialog
发布评论