如果互联网连接不可用则显示消息

Display message if internet connection is not available

我使用 Xamarin.Forms 为我的网站制作了一个简单的 WebView 应用程序。当设备无法访问互联网时,我会显示以下内容:

但是我想在设备上没有互联网访问时显示一条消息而不是上面的显示。

App.xaml.cs

using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace MyApp
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            MainPage = new MainPage();
        }
        protected override void OnStart()
        {
            // Handle when your app starts
        }
        protected override void OnSleep()
        {
            // Handle when your app sleeps
        }
        protected override void OnResume()
        {
            // Handle when your app resumes
        }
    }
}

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace MyApp
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(true)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Broswer.Source = "https://mywebpage.com/";
        }

        protected override bool OnBackButtonPressed()
        {
            if (Broswer.CanGoBack)
            {
                Broswer.GoBack();
                return true;
            }
            return false;
        }
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MyApp"
             x:Class="MyApp.MainPage">

    <WebView x:Name="Broswer" HeightRequest="1920" WidthRequest="1080"/>

</ContentPage>

您可以安装 Xam.Plugin.Connectivity NuGet 包。

在你的MainPage.xaml.cs

[DesignTimeVisible(true)]
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        if (CrossConnectivity.Current.IsConnected) {  
            Broswer.Source = "https://mywebpage.com/"; 
        } else {  
            DisplayAlert ("Alert", "Your alert", "OK");  
        } 
    }

    protected override bool OnBackButtonPressed()
    {
        if (Broswer.CanGoBack)
        {
            Broswer.GoBack();
            return true;
        }
        return false;
    }
}

您可以使用 Xamarin.Essentials: Connectivity 检查 Internet 连接。更简单。

1.install Xamarin.Essentials 到您的项目。

2.Use Connectivity

using Xamarin.Essentials;

var current = Connectivity.NetworkAccess;

if (current == NetworkAccess.Internet)
{
    // Connection to internet is available
}

3.You 也可以检查 connection profile 的类型,例如:

var profiles = Connectivity.ConnectionProfiles;
if (profiles.Contains(ConnectionProfile.WiFi))
{
    // Active Wi-Fi connection.
}

无论如何,我找到了解决问题的方法,方法是使用 Xam.Plugin.Connectivity

特别感谢@Jack Hua - MSFT,@Wilson Vargas。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Plugin.Connectivity;

namespace MyApp
{

    [DesignTimeVisible(true)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            CrossConnectivity.Current.ConnectivityChanged += Current_ConnectivityChanged;
        }

        protected async override void OnAppearing()
        {
            base.OnAppearing();

            if (!CrossConnectivity.Current.IsConnected)
            {
                await DisplayAlert("Error Title", "Error Msg", "OK");
            }
            else
            {
                Broswer.Source = "https://mypage.com/";
            }
        }

        private async void Current_ConnectivityChanged(object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e)
        {
            if (!e.IsConnected)
            {
                await DisplayAlert("Error Title", "Error Msg", "OK");
            }
            else
            {
                Broswer.Source = "https://mypage.com/";
            }
        }

        protected override bool OnBackButtonPressed() 
        {
            if (Broswer.CanGoBack)
            {
                Broswer.GoBack();
                return true;
            }
            return false;
        }
    }
}