none
win8 app中,如何通过MessageDialog,做到在程序的任何一个界面,当网络断开的时候给出用户提示并等待操作结果,根据客户点击的再执行相应操作?急!! RRS feed

  • 问题

  • 我现在的需求是想让应用程序在运行的过程中,无论处在哪个界面,都可以根据当前网络状态给出提示,当突然断网给出客户提示是否留在当前页还是回到登录界面。我的操作想法是在app.xaml.cs中注册网络改变事件,然后在断网的时候这样给出提示,不知道对不对,await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, ToUserPrompt);Dispatcher提示不存在,是不是在类里无法操作ui层,求高手指点,这样做是不是有问题,该如何改进才能实现?                                                      //网络状态给用户提示

            public async void ToUserPrompt()

            {

                MessageDialog mg = new MessageDialog("网络断开,是否留在本页?");

                mg.Commands.Add(new UICommand("Yes", null, 0));

                mg.Commands.Add(new UICommand("NO", null, 1));

                mg.DefaultCommandIndex = 0;

                mg.CancelCommandIndex = 1;

                var cellback = await mg.ShowAsync();

                switch (cellback.Id.ToString())

                {

                    case "0":

                       //处在断网状态

                        break;

                    case "1":

                        //回到登录界面

                        break;

                }

            }

    备注:这些都是写在app.xaml.cs中

    2013年4月27日 8:17

答案

  • 这个是我自己项目中的代码,希望对你有用:

       public class NetworkStatusMonitor
        {
    
            #region For Singleton
    
            private static NetworkStatusMonitor _CurrentNetworkStatusMonitor;
            public static NetworkStatusMonitor CurrentNetworkStatusMonitor
            {
                get
                {
                    if (_CurrentNetworkStatusMonitor == null)
                        _CurrentNetworkStatusMonitor = new NetworkStatusMonitor();
                    return _CurrentNetworkStatusMonitor;
                }
            }
    
            #endregion
    
            NetworkStatusChangedEventHandler networkStatusCallback = null;
            private static bool registeredNetworkStatusNotif = false;
            private CoreDispatcher _cd = Window.Current.CoreWindow.Dispatcher;
            private FullScreenPopup networkDisconnectAlert;
            private NetworkStatusMonitor()
            {
                string contentXaml = Resources.ResourcesLoader.Loader["NetworkStatusXamlContent"];
                networkDisconnectAlert = new FullScreenPopup(ConstKeys.NETWORK_MONITOR_KEY, contentXaml);
    
            }
    
            //
            //Register Network Status Change notifications
            //
            public void RegisterForNetworkStatusChangeNotif()
            {
                networkStatusCallback =
                    new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
                if (!registeredNetworkStatusNotif)
                {
                    NetworkInformation.NetworkStatusChanged += networkStatusCallback;
                    registeredNetworkStatusNotif = true;
                }
            }
    
            //
            //Unregister Network Status Change notifications
            //
            public void UnRegisterForNetworkStatusChangeNotif()
            {
                NetworkInformation.NetworkStatusChanged -= networkStatusCallback;
                registeredNetworkStatusNotif = false;
            }
    
            public async Task CheckInternetStatusAsync()
            {
                //network status changed
                try
                {
    
                    // get the ConnectionProfile that is currently used to connect to the Internet                
                    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    
                    if (InternetConnectionProfile == null)
                    {
                        AppSettings.Instance.Settings[AppSettings.GLOBAL_INTERNET_STATUS] = false;
                        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            (Window.Current.Content as Frame).IsEnabled = false;
                            networkDisconnectAlert.Show();
                        });
                    }
                    else
                    {
                        AppSettings.Instance.Settings[AppSettings.GLOBAL_INTERNET_STATUS] = true;
                        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            (Window.Current.Content as Frame).IsEnabled = true;
                            networkDisconnectAlert.Close();
                        });
                    }
                }
                catch (Exception ex)
                {
                    ex.WriteLog();
                }
            }
    
            //
            // Event handler for Network Status Change event
            // 
            async void OnNetworkStatusChange(object sender)
            {
                await CheckInternetStatusAsync();
            }
    
        }

    FullScreenPopup是我自己的一个全屏半透明Popup。  AppSettings.Instance.Settings[AppSettings.GLOBAL_INTERNET_STATUS] 只是一个全局的标记。


    Bob Bao

    Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
    你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。

    2013年4月28日 2:16
    版主

全部回复

  • 我界面的后台代码中,比如按钮事件中Dispatcher.RunAsync 是自动提示在Window.ui.core.coredispatcher DependencyObject.dispatcher的。但现在在类里就提示没有,提示可以引用的只有System.ServiceModel.Dispatcher,但这个里面就没有RunAsync 。所以现在不知道用什么办法可以实现弹出窗体,并等待操作完毕了。
    2013年4月27日 9:51
  • 我是写在外框架页面的
    2013年4月27日 10:34
  • @远眺的时光  你是如何实现的啊,能详细聊下吗,向你学习下。我的QQ564463141  我的skype  Billy 可以添加下吗

    2013年4月27日 12:25
  • 这个是我自己项目中的代码,希望对你有用:

       public class NetworkStatusMonitor
        {
    
            #region For Singleton
    
            private static NetworkStatusMonitor _CurrentNetworkStatusMonitor;
            public static NetworkStatusMonitor CurrentNetworkStatusMonitor
            {
                get
                {
                    if (_CurrentNetworkStatusMonitor == null)
                        _CurrentNetworkStatusMonitor = new NetworkStatusMonitor();
                    return _CurrentNetworkStatusMonitor;
                }
            }
    
            #endregion
    
            NetworkStatusChangedEventHandler networkStatusCallback = null;
            private static bool registeredNetworkStatusNotif = false;
            private CoreDispatcher _cd = Window.Current.CoreWindow.Dispatcher;
            private FullScreenPopup networkDisconnectAlert;
            private NetworkStatusMonitor()
            {
                string contentXaml = Resources.ResourcesLoader.Loader["NetworkStatusXamlContent"];
                networkDisconnectAlert = new FullScreenPopup(ConstKeys.NETWORK_MONITOR_KEY, contentXaml);
    
            }
    
            //
            //Register Network Status Change notifications
            //
            public void RegisterForNetworkStatusChangeNotif()
            {
                networkStatusCallback =
                    new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
                if (!registeredNetworkStatusNotif)
                {
                    NetworkInformation.NetworkStatusChanged += networkStatusCallback;
                    registeredNetworkStatusNotif = true;
                }
            }
    
            //
            //Unregister Network Status Change notifications
            //
            public void UnRegisterForNetworkStatusChangeNotif()
            {
                NetworkInformation.NetworkStatusChanged -= networkStatusCallback;
                registeredNetworkStatusNotif = false;
            }
    
            public async Task CheckInternetStatusAsync()
            {
                //network status changed
                try
                {
    
                    // get the ConnectionProfile that is currently used to connect to the Internet                
                    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
    
                    if (InternetConnectionProfile == null)
                    {
                        AppSettings.Instance.Settings[AppSettings.GLOBAL_INTERNET_STATUS] = false;
                        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            (Window.Current.Content as Frame).IsEnabled = false;
                            networkDisconnectAlert.Show();
                        });
                    }
                    else
                    {
                        AppSettings.Instance.Settings[AppSettings.GLOBAL_INTERNET_STATUS] = true;
                        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            (Window.Current.Content as Frame).IsEnabled = true;
                            networkDisconnectAlert.Close();
                        });
                    }
                }
                catch (Exception ex)
                {
                    ex.WriteLog();
                }
            }
    
            //
            // Event handler for Network Status Change event
            // 
            async void OnNetworkStatusChange(object sender)
            {
                await CheckInternetStatusAsync();
            }
    
        }

    FullScreenPopup是我自己的一个全屏半透明Popup。  AppSettings.Instance.Settings[AppSettings.GLOBAL_INTERNET_STATUS] 只是一个全局的标记。


    Bob Bao

    Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
    你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。

    2013年4月28日 2:16
    版主