我試圖在Windows 8中再次嘗試/取消對話框。對話框第一次顯示罰款,但再次單擊再次嘗試失敗,我得到一個拒絕訪問在調用ShowAsync。 我不知道爲什麼,但它奇怪的有時代碼工作正常,我沒有得到例外,當我把斷點。這裏真的很無知MessageDialog 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();
}
}
要預防 「未分配的局部變量 'asyncCommand' 的使用」,我不得不分配分配給asyncCommand時爲null。 –
備註:我有自己的任務隊列在一個線程中運行,我一次只能從一個線程執行一個ShowAsync。顯然,如果一個ShowAsync在第一幀中結束,第二個ShowAsync在第二幀中開始,隨機訪問拒絕錯誤可以彈出:/。儘管手動取消作品。 – RelativeGames
@clay我更新了上面的代碼 – Syler