【发布时间】: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