我正在尝试在 Windows 8 中实现重试/取消对话框。该对话框第一次显示正常,但是在单击重试并再次失败时,我在调用 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();
}
}