【问题标题】:Detect network changes in Xamarin Forms检测 Xamarin Forms 中的网络更改
【发布时间】:2019-05-14 06:09:44
【问题描述】:

我想检测用户何时在线或离线。

我正在使用CrossConnectivity 包来检测连接变化。

我必须连接到 VPN(确切地说是 Sonic Wall) 才能连接到我的服务器。

我的问题是:当我连接到我的服务器时,我需要切换应用程序才能连接到我的服务器。当我切换回我的应用程序时,函数 SyncFunction.SyncUser(host, database, contact, ipaddress, pingipaddress) 没有执行。连接更改功能不在我的App.xaml.cs 上,它在我的Main Menu Content page 上,因为我需要在我的主菜单而不是整个应用程序中执行同步功能。我该如何解决这个问题?

CrossConnectivity.Current.ConnectivityChanged += async (sender, args) =>
{
    var appdate = Preferences.Get("appdatetime", String.Empty, "private_prefs");

    if (string.IsNullOrEmpty(appdate))
    {
        Preferences.Set("appdatetime", DateTime.Now.ToString(), "private_prefs");
    }
    else
    {
        if (DateTime.Now >= DateTime.Parse(Preferences.Get("appdatetime", String.Empty, "private_prefs")))
        {
            Preferences.Set("appdatetime", DateTime.Now.ToString(), "private_prefs");

            if (CrossConnectivity.Current.IsConnected)
            {
                var ping = new Ping();
                var reply = ping.Send(new IPAddress(pingipaddress), 5000);

                if (reply.Status == IPStatus.Success)
                {
                    lblStatus.Text = "Syncing data to server";
                    lblStatus.BackgroundColor = Color.FromHex("#2bcbba");

                    await Task.Delay(5000);
                    SyncFunction.SyncUser(host, database, contact, ipaddress, pingipaddress);
                    lblStatus.Text = "Online - Connected to server";
                    lblStatus.BackgroundColor = Color.FromHex("#2ecc71");
                }
                else
                {
                    lblStatus.Text = "Online - Server unreachable. Connect to VPN";
                    lblStatus.BackgroundColor = Color.FromHex("#e67e22");
                }
            }
            else
            {
                lblStatus.Text = "Offline - Connect to internet";
                lblStatus.BackgroundColor = Color.FromHex("#e74c3c");
            }
        }
        else
        {
            await DisplayAlert("Application Error", "It appears you change the time/date of your phone. Please restore the correct time/date", "Got it");
            await Navigation.PopToRootAsync();
        }
    }

};

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    检测跨 VPN 的连接变化并不容易。

    解决方法是使用网络服务作为 ping。 如果您有一个带有 API 的后端,则可以定期执行此“ping”以确保网络和 API 可访问。

    除了连接检查之外,还要使用此解决方案

    1. 订阅连接已更改
    2. 当连接正常时,检查“ping 服务”

    通常在 mob 应用程序中,此“ping 端点”可能类似于“/about”。 此外,此特定服务可用于执行 App Mob 版本和 API 版本之间的兼容性版本检查。

    (另请参阅 Xamarin.Essentials 以将 CrossConnectivity 替换为 Xamarin.Essentials:连接性,https://docs.microsoft.com/fr-fr/xamarin/essentials/connectivity?context=xamarin%2Fxamarin-forms&tabs=android)

    【讨论】:

    • 问题:我如何知道我的应用是否有焦点
    • 查看 Xamarin.Forms 应用程序的生命周期。您可以使用 OnStart() / OnPause() / OnResume()。 docs.microsoft.com/fr-fr/xamarin/xamarin-forms/app-fundamentals/…
    • 如何在 MainMenu.xaml.cs 中添加 Onresume()
    • 否,OnResume() 仅在 App.xaml.cs 中可用。你可以把你的逻辑放在这个类中,或者使用像 Event 或 Messenger 这样的机制来通知你的特定类。
    • 哦,那是我的问题,当我连接到我的服务器并且我在我的其他应用程序中并切换回来时,网络检测的更改不起作用
    猜你喜欢
    • 2014-10-20
    • 2018-04-13
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    相关资源
    最近更新 更多