1

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

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:

Invalid window handle.

This API must be called from a thread with a CoreWindow or a window must have been set explicitly.

Is there any way to show a MessageDialog from that event?

Thought
  • 5,326
  • 7
  • 33
  • 69

3 Answers3

3

enter image description hereTry to add this:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
    MessageDialog msg= new MessageDialog("No Internet");
    msg.ShowAsync();
 });
Draken
  • 3,134
  • 13
  • 34
  • 54
2

Manuel Patrone answer is almost correct. just small fix for his answer.

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
async() =>
{
    MessageDialog msg= new MessageDialog("No Internet");
    await msg.ShowAsync();
 });
Emil
  • 6,411
  • 7
  • 62
  • 112
0

If you're running through the Desktop Bridge ensure that you're initializing your store context properly. See Using the StoreContext class with the Desktop Bridge for more information about it.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Proteux
  • 213
  • 2
  • 3