Jip Harthoorn
Jip Harthoorn

Reputation: 302

Xamarin switch between pages without loading new one

How do I switch bewtween pages without loading a new page in Xamarin, so that when I make a change on a page and I load it again in the same session, I can see the change I made, but when I close the app and open it again the change is gone?

**enter image description here**

As you can see, when I go to the "Doel sport" page and I go back to the "Doel gewicht" page, the radio button is still selected. But when I then go back to "Doel sport" page, the radio button is not selected anymore (and the page loads two times, I don't know why?)

"Doel gwicht" page xaml:

<controls:AnimationPage
        xmlns:controls="clr-namespace:FormsControls.Base;assembly=FormsControls.Base" xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="TimeToSport.Views.GegevensGewichtDoel"
        xmlns:syncfusion="clr-namespace:Syncfusion.XForms.Buttons;assembly=Syncfusion.Buttons.XForms"
        Title="Doel gewicht">


<ContentPage.ToolbarItems>
    <ToolbarItem Text="Volgende" Clicked="Volgende_clicked">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource">
                <On Platform="UWP" Value="volgende.png"/>
            </OnPlatform>
        </ToolbarItem.Icon>
    </ToolbarItem>
</ContentPage.ToolbarItems>

<ContentPage.Content>

    <StackLayout Orientation="Horizontal"  
                 HorizontalOptions="FillAndExpand"  
                 VerticalOptions="FillAndExpand"  
                 Padding="10,30,0,0"  
                 Spacing="10">


        <syncfusion:SfRadioGroup x:Name="radioGroup">

            <Label Text="Wat is je doel m.b.t. gewicht?"  TextColor="Black" FontSize="17"/>
            <syncfusion:SfRadioButton x:Name="btn_Afvallen" Text="Afvallen" TextColor="Gray" StateChanged="RadioButton_StateChanged"/>
            <syncfusion:SfRadioButton x:Name="btn_OpGewichtBlijven" TextColor="Gray" Text="Op gewicht blijven" StateChanged="RadioButton_StateChanged"/>
            <syncfusion:SfRadioButton x:Name="btn_Aankomen" TextColor="Gray" Text="Aankomen" StateChanged="RadioButton_StateChanged"/>
        </syncfusion:SfRadioGroup>    
         </StackLayout>
     </ContentPage.Content>
 </controls:AnimationPage>

"Doel gwicht" page cs:

        void CreateRadioButtons()
    {
        SfRadioGroup radioGroup = new SfRadioGroup();
        SfRadioButton btn_Afvallen = new SfRadioButton();
        btn_Afvallen.IsChecked = false;
        btn_Afvallen.Text = "Afvallen";
        btn_Afvallen.StateChanged += RadioButton_StateChanged;
        SfRadioButton btn_OpGewichtBlijven = new SfRadioButton();
        btn_OpGewichtBlijven.Text = "Op gewicht blijven";
        btn_OpGewichtBlijven.StateChanged += RadioButton_StateChanged;
        SfRadioButton btn_Aankomen = new SfRadioButton();
        btn_Aankomen.Text = "Aankomen";
        btn_Aankomen.StateChanged += RadioButton_StateChanged;
        radioGroup.Children.Add(btn_Afvallen);
        radioGroup.Children.Add(btn_OpGewichtBlijven);
        radioGroup.Children.Add(btn_Aankomen);
    }

    private void RadioButton_StateChanged(object sender, StateChangedEventArgs e)
    {
        Navigation.PushAsync(new GegevensSportDoel());
    }

    void Volgende_clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new GegevensSportDoel());
    }

"Doel Sport" page xaml:

<controls:AnimationPage.PageAnimation>
    <controls:PushPageAnimation Duration="Medium" Subtype="FromRight" />
</controls:AnimationPage.PageAnimation>

<ContentPage.ToolbarItems>
    <ToolbarItem Text="Klaar" Clicked="Klaar_clicked">
        <ToolbarItem.Icon>
            <OnPlatform x:TypeArguments="FileImageSource">
                <On Platform="UWP" Value="volgende.png"/>
            </OnPlatform>
        </ToolbarItem.Icon>
    </ToolbarItem>
</ContentPage.ToolbarItems>

<ContentPage.Content>

    <StackLayout Orientation="Horizontal"  
                 HorizontalOptions="FillAndExpand"  
                 VerticalOptions="FillAndExpand"  
                 Padding="10,30,0,0"  
                 Spacing="10">


        <syncfusion:SfRadioGroup x:Name="radioGroup">

            <Label Text="Wat is je doel m.b.t. sport?"  TextColor="Black" FontSize="17"/>
            <syncfusion:SfRadioButton x:Name="btn_SterkerWorden" Text="Afvallen" TextColor="Gray" />
            <syncfusion:SfRadioButton x:Name="btn_ConditieOpbouwen" TextColor="Gray" Text="Op gewicht blijven" />
            <syncfusion:SfRadioButton x:Name="btn_GeenVanBeide" TextColor="Gray" Text="Aankomen"/>
        </syncfusion:SfRadioGroup>


         </StackLayout>
     </ContentPage.Content>
 </controls:AnimationPage>

"Doel Sport" page cs:

    void CreateRadioButtons()
    {
        SfRadioGroup radioGroup = new SfRadioGroup();
        SfRadioButton btn_Afvallen = new SfRadioButton();
        btn_Afvallen.IsChecked = false;
        btn_Afvallen.Text = "Afvallen";
        SfRadioButton btn_OpGewichtBlijven = new SfRadioButton();
        btn_OpGewichtBlijven.Text = "Op gewicht blijven";
        SfRadioButton btn_Aankomen = new SfRadioButton();
        btn_Aankomen.Text = "Aankomen";
        radioGroup.Children.Add(btn_Afvallen);
        radioGroup.Children.Add(btn_OpGewichtBlijven);
        radioGroup.Children.Add(btn_Aankomen);
    }

    void Klaar_clicked(object sender, EventArgs e)
    {
        App.Current.MainPage = new NavigationPage(new ItemsPage());
    }

Upvotes: 1

Views: 766

Answers (1)

Jason
Jason

Reputation: 89204

you simply need to maintain a reference to the new Page object instead of creating a new one every time

ItemsPage newPage = null;

void Klaar_clicked(object sender, EventArgs e)
{
    if (newPage == null) {
      newPage = new ItemsPage()
    }

    App.Current.MainPage = new NavigationPage(newPage);
}

Upvotes: 1

Related Questions