Android 表单如何显示internet连接状态?
我的主页是一个旋转木马页面,它包含三个ContentPage类型的页面Android 表单如何显示internet连接状态?,android,ios,xamarin,xamarin.android,xamarin.forms,Android,Ios,Xamarin,Xamarin.android,Xamarin.forms,我的主页是一个旋转木马页面,它包含三个ContentPage类型的页面 <CarouselPage ...some namespaces... <CarouselPage.Children> <pages:HomePageA /> <pages:HomePageB /> <pages:HomePageC /> </CarouselPage.Children>
<CarouselPage
...some namespaces...
<CarouselPage.Children>
<pages:HomePageA />
<pages:HomePageB />
<pages:HomePageC />
</CarouselPage.Children>
</CarouselPage>
启动屏幕消失后,消息框不会显示。根据调试过程中的单步代码,我假设它会显示出来,然后在启动屏幕消失之前消失。
所以我试着将ConnectivityPlugin代码放入HomePageA:ContentPage,HomePageA:ContentPage是HomePageA:CarouselPage的子项之一。像这样:
public partial class HomePageA : ContentPage
{
public HomePageA()
{
InitializeComponent();
if (IsConnectionAvailable())
{
// download content from external db to device (SQLite db)
DisplayAlert("Internet connection found.", "Wait for the application data to update.", "OK");
}
else
{
DisplayAlert("No internet connection found.", "Application data may not be up to date. Connect to a working network.", "OK");
}
}
public bool IsConnectionAvailable()
{
if (!CrossConnectivity.IsSupported)
return false;
bool isConnected = CrossConnectivity.Current.IsConnected;
//return CrossConnectivity.Current.IsConnected;
return isConnected;
}
private void RegistrationButton_Clicked(object sender, System.EventArgs e)
{
}
}
现在,当我运行应用程序时,会显示HomePageA,但没有消息框。只有在我单击菜单并选择Home(有效地选择HomePageA)后,消息框才会弹出。
同样的事情再次发生:
启动屏幕消失后,消息框不会显示。根据调试过程中的单步代码,我假设它会显示出来,然后在启动屏幕消失之前消失。
有人能给我解释一下这种行为吗?
启动屏幕消失后,如何使消息框显示?
谢谢大家 =======================================U p D A T E=====================
Pavan没有按照我希望的方式解释这个问题。对于所有善良的人们,我发现了两个类似的问题和答案,并给出了充分的解释。
可以找到一个像样的解释,这是另一个
但是,我将接受Pavan的答案,因为它是有效的,而且当时没有提供更好的答案。DisplayAlert用于在UI线程上执行某些操作。它是从后台线程调用的,以便操作UI,这只能在UI线程上完成 请尝试以下代码以显示警报消息
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
App.Current.MainPage.DisplayAlert("Internet connection found.", "Wait for the application data to update.", "OK");
});
这对你有帮助你能详细说明一下吗?我把那个片段放在哪里?在“if语句”中?哪一班?谢谢。是的,把它放在if和else中。我不是想打听,兄弟,但你能解释一下当你强制在主UI线程上运行某些东西时到底发生了什么吗?我正在用谷歌搜索它,但我也欢迎您的解释。@PaxForce,这是用来在UI线程上做一些事情的。因此,它通常是从后台线程调用的,以便操作UI,这只能在UI线程上完成。@PaxForce,我也用上面的注释更新了我的答案。我已经在我的评论中解释了你的问题。
Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
App.Current.MainPage.DisplayAlert("Internet connection found.", "Wait for the application data to update.", "OK");
});