简体   繁体   English

MessageDialog在调用时崩溃-Windows Phone Store应用

[英]MessageDialog crashes when called - Windows Phone Store app

I am having a message dialog to prompt the user when there is no internet. 当没有互联网时,我有一个消息对话框提示用户。

public async void validateInternet()
{
    if (!isInternet())
    {
        Action cmdAction = null;
        MessageDialog _connectAlert = new MessageDialog("We are currently experiencing difficulties with your conectivity. Connect to internet and refresh again.", "No internet");
        _connectAlert.Commands.Add(new UICommand("Retry", (x) => { cmdAction = () => validateInternet(); }));
        _connectAlert.DefaultCommandIndex = 0;
        _connectAlert.CancelCommandIndex = 1;
        await _connectAlert.ShowAsync();
        cmdAction.Invoke();
    }
}

public static bool isInternet()
{
    ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
    bool internet = connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    return internet;
}

Now, I call this function validateInternet when MainPage has loaded. 现在,当MainPage已加载时,我将此函数称为validateInternet The app opens the message dialog, and crashes immediately. 该应用程序打开消息对话框,并立即崩溃。 What's wrong with the code? 代码有什么问题?

A A first chance exception of type 'System.UnauthorizedAccessException' occurred in BusTrack.exe Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) A first chance exception of type 'System.UnauthorizedAccessException' occurred in BusTrack.exe Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) A first chance exception of type 'System.UnauthorizedAccessException' occurred in BusTrack.exe Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) is thrown at A first chance exception of type 'System.UnauthorizedAccessException' occurred in BusTrack.exe Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))引发于

await _connectAlert.ShowAsync();

As @yasen rightly pointed out, the validateInternet() was called both in MainPage_loaded() and OnNavigatedTo() . 由于正确地@yasen指出的那样, validateInternet()是无论是在叫MainPage_loaded()OnNavigatedTo()

Hence, System.UnauthorizedAccessException occurs since both of those methods will try to show a messageDialog . 因此,发生System.UnauthorizedAccessException ,因为这两个方法都将尝试显示messageDialog So, never try to call two message dialogs at the same time. 因此,永远不要尝试同时调用两个消息对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM