简体   繁体   English

在Windows应用商店中从app.xaml.cs显示MessageDialog

[英]Show MessageDialog from app.xaml.cs in Windows Store app

Im trying to show a message to the user when connection to the internet is lost 我试图在失去互联网连接时向用户显示消息

I have this method on my App.xaml.cs 我在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());
        }

    }

and this other one 还有另外一个

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

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

When i cause a connection lost ( disable my internet connection ) the ShowBox method gets called, and i get this exception on it: 当我导致连接丢失(禁用我的互联网连接)时,将调用ShowBox方法,并且我在其上收到此异常:

Invalid window handle. 无效的窗口句柄。

This API must be called from a thread with a CoreWindow or a window must have been set explicitly. 必须从具有CoreWindow的线程中调用此API,或者必须已显式设置了窗口。

Is there any way to show a MessageDialog from that event? 有什么办法可以显示该事件的MessageDialog吗?

在此处输入图片说明 Try to add this: 尝试添加以下内容:

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

Manuel Patrone answer is almost correct. Manuel Patrone的答案几乎是正确的。 just small fix for his answer. 只是他的答案的小修正。

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

If you're running through the Desktop Bridge ensure that you're initializing your store context properly. 如果您正在运行Desktop Bridge,请确保正确初始化商店上下文。 See Using the StoreContext class with the Desktop Bridge for more information about it. 有关更多信息,请参阅将StoreContext类与Desktop Bridge一起使用

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

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