调用时,MessageDialog崩溃 - Windows Phone Store应用程序

时间:2014-12-04 06:51:50

标签: c# windows-runtime windows-phone-8.1

我正在建立一个消息对话框,以便在没有互联网时提示用户。

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;
}

现在,我在MainPage加载时调用此函数validateInternet。该应用程序打开消息对话框,并立即崩溃。代码有什么问题?

A 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();

1 个答案:

答案 0 :(得分:1)

正如@yasen正确指出的那样,validateInternet()MainPage_loaded()同时调用OnNavigatedTo()

因此,System.UnauthorizedAccessException会发生,因为这两种方法都会尝试显示messageDialog。所以,永远不要试图同时调用两个消息对话框。