Issue
I need to reload my Xamarin IOS / Android app after turn on my internet connection (turn on wifi or mobile data)
I have code to check whether internet is available or not
Just I need to refresh App page
More info
App shell contains
<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# Code
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
IsInternetNotAvailable = e.NetworkAccess != NetworkAccess.Internet;
if (IsInternetNotAvailable == false)
{
}
}
Solution
You can use MessagingCenter to publish a message when the network changed:
private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
IsInternetNotAvailable = e.NetworkAccess != NetworkAccess.Internet;
if (IsInternetNotAvailable == false)
{
MessagingCenter.Send<MainPage, string>(this, "NetWorkChange", "NotAvailable");
}
}
And subscribe to the message in all other page you need to update. Unsubscribe it in OnDisappearing
method:
protected override void OnAppearing()
{
base.OnAppearing();
MessagingCenter.Subscribe<MainPage, string>(this, "Hi", async (sender, arg) =>
{
await DisplayAlert("Message received", "arg=" + arg, "OK");
if (arg == "NotAvailable")
{
}
else
{
}
});
}
protected override void OnDisappearing()
{
base.OnDisappearing();
MessagingCenter.Unsubscribe<MainPage, string>(this, "Hi");
}
Answered By - nevermore
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.