【问题标题】:Show MessageDialog from app.xaml.cs in Windows Store app在 Windows 应用商店应用中显示来自 app.xaml.cs 的 MessageDialog
【发布时间】:2015-10-29 12:28:06
【问题描述】:

我试图在失去互联网连接时向用户显示一条消息

我的App.xaml.cs有这个方法

static async void NetworkInformation_NetworkStatusChanged(object sender)
    {

        //Get the Internet connection profile
        //ConnectionProfile connectionProfileInfo = null;
        try
        {
            ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

            if (InternetConnectionProfile == null)
            {
                ApplicationData.Current.LocalSettings.Values["INTERNET"] = false;

                ShowBox("Internet Lost");

                Frame rootFrame = Window.Current.Content as Frame;
                rootFrame.Navigate(typeof(LoginPage));
            }
            else
            {
                ApplicationData.Current.LocalSettings.Values["INTERNET"] = true;
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Unexpected exception occurred: " + ex.ToString());
        }

    }

还有这个

public async static void ShowBox(string msg)
    {
        try
        {

            await new MessageDialog(msg, "No Internet").ShowAsync();
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
        }
    }

当我导致连接丢失(禁用我的互联网连接)时,ShowBox 方法被调用,我得到了这个异常:

窗口句柄无效。

必须从具有 CoreWindow 或窗口的线程调用此 API 必须明确设置。

有什么方法可以显示该事件中的 MessageDialog 吗?

【问题讨论】:

标签: c# xaml windows-runtime windows-store-apps


【解决方案1】:

尝试添加这个:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
    MessageDialog msg= new MessageDialog("No Internet");
    msg.ShowAsync();
 });

【讨论】:

    【解决方案2】:

    Manuel Patrone 的回答几乎是正确的。只是他的答案的小修复。

    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
    async() =>
    {
        MessageDialog msg= new MessageDialog("No Internet");
        await msg.ShowAsync();
     });
    

    【讨论】:

      【解决方案3】:

      如果您正在通过桌面桥运行,请确保您正在正确初始化您的商店上下文。有关它的更多信息,请参阅Using the StoreContext class with the Desktop Bridge

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        • 2015-11-22
        • 1970-01-01
        • 1970-01-01
        • 2013-10-27
        • 2016-05-25
        相关资源
        最近更新 更多