如果没有互联网连接,则显示消息

时间:2019-08-05 18:24:20

标签: c# xamarin.forms webview xamarin.android xamarin.ios

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

Image 1

但是如果设备上没有Internet访问权限,我想显示一条消息,而不是上面的显示。

App.xaml.cs

df = df.dropna(subset=["dt_mvmt"])

MainPage.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

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;
        }
    }
}

3 个答案:

答案 0 :(得分:3)

您可以安装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;
    }
}

答案 1 :(得分:2)

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

1。将Xamarin.Essentials安装到您的项目中。

2。Use Connectivity

using Xamarin.Essentials;

var current = Connectivity.NetworkAccess;

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

3。您还可以像以下这样检查connection profile的类型:

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

答案 2 :(得分:0)

无论如何,我使用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;
        }
    }
}