Is there any way to reload Main page every time when user changes an option inside the Settings page?

Viewed 33

I have a settings page where I can set the global variable (0-Metric , 1-Imperial)

I have main page that I need to reload and change text values everytime this variable is changed (from picker inside Settings page)

I tried OnAppearing() function. But the text stays the same. (Code works only once when starting app)

Code (AboutPage):

protected override void OnAppearing()
    {

        base.OnAppearing();

        int system = ((App)App.Current).system_global; //global varible changed from settings page
        test.Text = system.ToString(); //used for debug

        //Metric
        if (system == 0)
        {
            devide = 100;

            Label_distance.Text = "Distance travelled (km):";
        }

        //Imperial
        else if (system == 1)
        {
            devide = 1;

            Label_distance.Text = "Distance travelled (miles):";
    }

Code (Settings page):

private void Picker_SelectedIndexChanged(object sender, EventArgs e)
    {
        system = Picker_Settings.SelectedIndex; //change global variable to 0 or 1
        test.Text = "Update:" + system.ToString(); //debug
    }

Is there any way to reload AboutPage every time when user changes an option inside the picker?

1 Answers

What is probably happening is that you're navigating back to the other page by Popping the stack. This means that the most recent page is just getting removed but the previous page isn't being told to reload. It's just showing again.

So I'd recommend re-navigating back to the previous page to re-initialise everything. The other option is to make use of a Messaging center and use that when you change your values. That way you can trigger the data refresh from the Settings page as well

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/messaging-center

Related