Xamarin在页面之间切换而不加载新页面

时间:2018-10-12 11:40:06

标签: c# xaml xamarin xamarin.forms

如何在页面之间切换而不在Xamarin中加载新页面,所以当我在页面上进行更改并在同一会话中再次加载它时,我可以看到所做的更改,但是当我关闭页面时应用程序,然后再次打开,更改就消失了?

**enter image description here**

如您所见,当我转到“ Doel sport”页面并回到“ Doel gewicht”页面时,单选按钮仍处于选中状态。但是,当我返回“ Doel sport”页面时,不再选择单选按钮(并且该页面加载了两次,我不知道为什么?)

“ Doel gwicht”页面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”页面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”页面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”页面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());
    }

1 个答案:

答案 0 :(得分:1)

您只需要维护对新Page对象的引用,而不是每次都创建一个新对象

ItemsPage newPage = null;

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

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