检查互联网连接

时间:2014-09-03 16:47:27

标签: c# wpf xaml windows-8.1

我有一个自定义用户控件,如果我的用户已连接到应用栏中的互联网,则会以可视方式显示。问题是如果用户没有连接,那么四个连接条在视觉上变成红色以让我的用户知道,如果他们重新连接,它应该变为绿色。相反,如果他们重新连接,他们必须导航到另一个页面才能看到颜色已变为绿色。

这是我的代码:

        public Connection ()
    {
        this.InitializeComponent();            
        if (IsConnected)
        {
            connectionView.Icon.Name = "FourBars";
            connectionView.Foreground = new SolidColorBrush(Windows.UI.Colors.LimeGreen);
            connectionView.Label = "Connected";
        }
        else if (!IsConnected)
        {
            connectionView.Icon.Name = "OneBar";
            connectionView.Foreground = new SolidColorBrush(Windows.UI.Colors.Red);
            connectionView.Label = "Not Connected";
        }

    }

    public bool IsConnected
    {
        get
        {
            ConnectionProfile connectionProfile = NetworkInformation.GetInternetConnectionProfile();
            return (connectionProfile != null &&
                    connectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
        }
    }

    private async void ConnectionView_OnClick (object sender, RoutedEventArgs e)
    {
        if (IsConnected)
        {
            var messageDialog =
                new MessageDialog("You are Connected!");
            // Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers 
            messageDialog.Commands.Add(new UICommand("Close"));

            // Set the command that will be invoked by default 
            messageDialog.DefaultCommandIndex = 0;

            // Set the command to be invoked when escape is pressed 
            messageDialog.CancelCommandIndex = 1;

            // Show the message dialog 
            await messageDialog.ShowAsync();
        }
        else if (!IsConnected)
        {
            var messageDialog =
                 new MessageDialog(
                     "No internet connection has been found. Please connect to the internet!");
            // Add commands and set their callbacks; both buttons use the same callback function instead of inline event handlers 
            messageDialog.Commands.Add(new UICommand("Close"));

            // Set the command that will be invoked by default 
            messageDialog.DefaultCommandIndex = 0;

            // Set the command to be invoked when escape is pressed 
            messageDialog.CancelCommandIndex = 1;

            // Show the message dialog 
            await messageDialog.ShowAsync();
        }
    }

}

所以我需要能够让我的应用栏中的指示器在重新连接时自动更改颜色。

0 个答案:

没有答案