始终检查是否有 Internet 连接 Xamarin 表单 [英] Always Check if there is Internet Connection Xamarin forms

查看:17
本文介绍了始终检查是否有 Internet 连接 Xamarin 表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 xamarin 表单应用程序,我想每秒检查是否有互联网连接,如果连接丢失,程序应该转到不同的页面.我正在使用插件 "Xam.Plugin.Connectivity" ,但没有做我想要的.可以做我想做的事吗?

I am doing a xamarin forms app, i want to check every second if there is a internet connection, if the connection is lost, the program should go to diferent page. I am using the plugin "Xam.Plugin.Connectivity" , but doesnt do what i want. Is possible to do what i want?

推荐答案

编辑:使用新的 Xamarin Essentials Connectivity plugin,只需按照那里的说明操作:

Edit: this can be easily done with the new Xamarin Essentials Connectivity plugin, just follow the instructions there :D

在您的 App.cs(或 App.xaml.cs)中创建一个方法,如下所示:

Create a method in your App.cs (or App.xaml.cs) like this:

private async void CheckConnection()
{
    if(!CrossConnectivity.Current.IsConnected)
         await Navigation.PushAsync(new YourPageWhenThereIsNoConnection());
    else
         return;
}
    

并在您的主应用程序方法中使用它,如下所示:

And use it on your main app method like this:

public App()
{
    InitializeComponent();

    var seconds = TimeSpan.FromSeconds(1);
    Xamarin.Forms.Device.StartTimer(seconds,
        () =>
        {
             CheckConnection();
        });
}

这篇关于始终检查是否有 Internet 连接 Xamarin 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆