MessageDialog 在被调用时崩溃 - Windows Phone 应用商店应用

本文关键字:应用 Phone Windows 崩溃 调用 MessageDialog | 更新日期: 2023-09-27 18:33:07

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

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

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

一个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();

MessageDialog 在被调用时崩溃 - Windows Phone 应用商店应用

正如

@yasen正确指出的那样,validateInternet()MainPage_loaded()OnNavigatedTo()中都被称为。

因此,System.UnauthorizedAccessException发生,因为这两种方法都将尝试显示messageDialog 。因此,切勿尝试同时调用两个消息对话框。