我试图在Windows 8中实现再次尝试/取消对话框。对话框第一次显示罚款,但再次单击再次失败,我得到一个访问被拒绝exception调用ShowAsync。 我不知道为什么,但奇怪的是,有时代码工作正常,我没有得到例外,当我把断点。 这里真的很无能
这里是代码。
async void DismissedEventHandler(SplashScreen sender, object e) { dismissed = true; loadFeeds(); } private async void loadFeeds() { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { try { RSSDataSource rssDataSource = (RSSDataSource)App.Current.Resources["RSSDataSource"]; if (rssDataSource != null) { await rssDataSource.DownloadFeeds(); await rssDataSource.GetFeedsAsync(); } AdDataSource ads = (AdDataSource)App.Current.Resources["AdDataSource"]; if (ads != null) { await ads.DownloadAds(); } rootFrame.Navigate(typeof(HomePageView)); Window.Current.Content = rootFrame; } catch { ShowError(); } }); } async void ShowError() { // There was likely a problem initializing MessageDialog msg = new MessageDialog(CONNECTION_ERROR_MESSAGE, CONNECTION_ERROR_TITLE); // Add buttons and set their command handlers msg.Commands.Add(new UICommand(COMMAND_LABEL_RETRY, new UICommandInvokedHandler(this.CommandInvokedHandler))); msg.Commands.Add(new UICommand(COMMAND_LABEL_CLOSE, new UICommandInvokedHandler(this.CommandInvokedHandler))); // Set the command to be invoked when a user presses 'ESC' msg.CancelCommandIndex = 0; await msg.ShowAsync(); } /// <summary> /// Callback function for the invocation of the dialog commands /// </summary> /// <param name="command">The command that was invoked</param> private void CommandInvokedHandler(IUICommand command) { string buttonLabel = command.Label; if (buttonLabel.Equals(COMMAND_LABEL_RETRY)) { loadFeeds(); } else { // Close app Application.Current.Exit(); } }
好吧,我找到了一个快速解决方案,
定义一个IAsyncOperation类varialble
IAsyncOperation<IUICommand> asyncCommand = null;
并将其设置为MessageDialog的ShowAsync方法
asyncCommand = msg.ShowAsync();
在用于重试的命令处理程序中,再次尝试检查asyncCommand是否为空,并在必要时取消最后的操作
if(asyncCommand != null) { asyncCommand.Cancel(); }
如果有更好的方法,请让我来。
我迟到了,但是这里有一个方法,你可以随时等待对话框的结果,也不必担心连续调用太多:
首先在您的应用程序中定义一个静态变量和方法:
private static IAsyncOperation<IUICommand> messageDialogCommand = null; public async static Task<bool> ShowDialog(MessageDialog dlg) { // Close the previous one out if (messageDialogCommand != null) { messageDialogCommand.Cancel(); messageDialogCommand = null; } messageDialogCommand = dlg.ShowAsync(); await messageDialogCommand; return true; }
现在,您可以传入任何对话框并始终等待执行。 这就是为什么这返回一个布尔而不是无效的。 您不必担心倍数之间的冲突。 为什么不让这个方法接受一个字符串? 由于标题和Yes / No命令处理程序,您可以将其分配到您正在使用的特定对话框中。
调用如:
await App.ShowDialog(new MessageDialog("FOO!"));
要么
var dlg = new MessageDialog("FOO?", "BAR?"); dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(YesHandler))); dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(NoHandler))); await App.ShowDialog(dlg);
MSDN论坛上有一个答案,可以帮助你在这里。
我有一个类似的问题,但我的showAsync调用是在单独的线程单独的功能,所以我不能放下done()在那里我不认为…
前几天我面对同样的问题,我解决它等待ShowAsync,然后再次打开MessageDialog的递归调用。
public async void ShowDlg(){ Action cmdAction = null; var msgDlg = new MessageDialog("Content.", "Title"); msgDlg.Commands.Add(new UICommand("Retry", (x) => { cmdAction = () => ShowDlg(); })); msgDlg.Commands.Add(new UICommand("Cancel", (x) => { cmdAction = () => <Action associated with the cancel button>; })); msgDlg.DefaultCommandIndex = 0; msgDlg.CancelCommandIndex = 1; await msgDlg.ShowAsync(); cmdAction.Invoke(); }
希望这个帮助!
另一个方案
private bool _messageShowing = false; // ... if (!_messageShowing) { _messageShowing = true; var messageDialog = new MessageDialog("Example"); // ... "messageDialog" initialization Task<IUICommand> showMessageTask = messageDialog.ShowAsync().AsTask(); await showMessageTask.ContinueWith((showAsyncResult) => { _messageShowing = false; }); }