问题描述
打开互联网连接(打开wifi或移动数据)后,我需要重新加载Xamarin IOS / Android应用
我只需要刷新应用页面
更多信息
应用程序外壳包含
<TabBar>
<Tab Title="Home" Route="Home">
<Tab.Icon>
<FontimageSource
x:Name="home"
Glyph=""
FontFamily="{StaticResource FontAwesomeSolid}"
Size="20"
/>
</Tab.Icon>
<ShellContent ContentTemplate="{DataTemplate local:Home}" />
</Tab>
....................
</TabBar>
C#代码
private void Connectivity_ConnectivityChanged(object sender,ConnectivityChangedEventArgs e)
{
IsInternetNotAvailable = e.NetworkAccess != NetworkAccess.Internet;
if (IsInternetNotAvailable == false)
{
}
}
解决方法
当网络改变时,您可以使用MessagingCenter发布消息:
private void Connectivity_ConnectivityChanged(object sender,ConnectivityChangedEventArgs e)
{
IsInternetNotAvailable = e.NetworkAccess != NetworkAccess.Internet;
if (IsInternetNotAvailable == false)
{
MessagingCenter.Send<MainPage,string>(this,"NetWorkChange","NotAvailable");
}
}
并在您需要更新的所有其他页面中订阅消息。通过OnDisappearing
方法退订:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<MainPage,"Hi",async (sender,arg) =>
{
await DisplayAlert("Message received","arg=" + arg,"OK");
if (arg == "NotAvailable")
{
}
else
{
}
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<MainPage,"Hi");
}