简体   繁体   中英

I want to reload the App IOS/ Android after coming back from offline to online - Xamarin

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="&#xe065;"
                             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)
            {

            }
     }

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM