Xamarin.Form显示如何显示Internet连接状态



我的主页是一个轮廓页,其中包含三页类型的contentpage。

<CarouselPage
    ...some namespaces...
    <CarouselPage.Children>
        <pages:HomePageA />
        <pages:HomePageB />
        <pages:HomePageC />
    </CarouselPage.Children>
</CarouselPage>

我正在使用Jamesmontemagno的ConnectivityPlugin检查设备是否具有Internet连接:

  public partial class HomePage : CarouselPage
  {
    public HomePage()
    {
      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;
    }
  }

Splash屏幕消失后,消息框未显示。根据调试时的代码,我认为它有点显示出来,然后在飞溅屏幕消失之前就消失了。


因此,我试图将ConnectivityPlugin代码放入HomePagea:ContentPage,其中一个主页: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)之后,消息框弹出。

同样的事情再次发生:
Splash屏幕消失后,消息框未显示。根据调试时的代码,我认为它有点显示出来,然后在飞溅屏幕消失之前就消失了。

有人可以向我解释这种行为吗?
在飞溅屏幕消失之后,如何使消息框出现?

谢谢大家。

=====================================================


Pavan没有像我想解释的那样解释问题。对于你们所有的好人,我发现了几个类似的问题和答案,并提供了足够的解释。

可以在此处找到一个不错的解释 there and there 的另一个。

但是,我会接受帕文的答案,因为它有效,当时没有更好的答案。

用于在UI线程上执行某些操作。为了操纵UI,它是从背景线程中调用的,只能在UI线程上完成。

尝试以下代码以获取显示警报消息,

Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
{
    App.Current.MainPage.DisplayAlert("Internet connection found.", "Wait for the application data to update.", "OK");
});

它将帮助您

最新更新