【问题标题】:Display message if internet connection is not available如果互联网连接不可用,则显示消息
【发布时间】:2019-12-13 07:21:05
【问题描述】:

我使用 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>

【问题讨论】:

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


    【解决方案1】:

    您可以安装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.安装Xam.Plugin.Connectivity2.MainPage.xaml.cs上添加库
    【解决方案2】:

    您可以使用Xamarin.Essentials: Connectivity 来检查互联网连接。这更容易。

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

    【讨论】:

    • 也没有用。当设备没有互联网连接时不会出现警报,它仍然显示我在上面发布的图像。我按照你说的所有步骤。 1. 安装Xamarin.Essentials 2.MainPage.xaml.cs 上添加库3.public MainPage() 上制作一个简单的if (Connectivity.NetworkAccess != NetworkAccess.Internet) { DisplayAlert("Alert", "No Internet", "OK"); }跨度>
    • @Fotic 它应该可以工作。您是否按照get-started 部分所述访问了连接功能?
    【解决方案3】:

    无论如何我找到了解决问题的方法,使用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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多