简体   繁体   中英

How can i navigate pages inside a page in Windows Phone Application

i am creating a WindowsPhone Application for which i have to navigate to some pages inside my app, but i dont want to leave the MainPage.

Its like a small page inside the MainPage, is there any way possible?

I tried creating User Control for the same, but the disadvantage is the User Control loads just after the MainPage is loaded, but i dont want the same. I want to click somewhere in my app then it loads the next page inside (keeping my application fast) instead of loading all user controls at once on MainPage load event.

If there is any alternative way you can help, please respond.

There is no way to navigate to a frame while first frame remains open. and as you cannot use popup due to pivot/panorama. you have to stick with the user control approach. you can optimize your xaml for performance below are some tips on how to optimize xaml

Optimize loading XAML

Edit

    MyUserControl1 control = new MyUserControl1();
    control.DataContext = //bindable data from usercontrol
    this.ContentRoot.Children.Add(control);

Just have a Frame inside your MainPage. Navigate to other pages using that frame.

 <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid x:Name="TitlePanel" Grid.Row="0">
            <StackPanel Orientation="Vertical">
                <TextBlock Text="Title" />
                <Button Click="Button_Click"
                        Content="Page1"
                        Tag="SharedFrame.BasicPage1" />
                <Button Click="Button_Click"
                        Content="Page2"
                        Tag="SharedFrame.BasicPage2" />
            </StackPanel>
        </Grid>
        <Grid x:Name="ContentPanel" Grid.Row="1">
            <Frame x:Name="rootFrame" />
        </Grid>
    </Grid>

This is how you navigate from the code behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
               Button b = sender as Button;

                if (b != null && b.Tag != null)
                {
                      Type pageType = Type.GetType(b.Tag.ToString());


                       if (pageType != null && rootFrame.CurrentSourcePageType != pageType)
                       {

                            rootFrame.Navigate(pageType);
                       }
                 }   
}

BasicPage1 and BasicPage2 are the pages I want to navigate to, inside the MainPage.

If your main page has enough free space for your "other page", you can try this: Place another Frame in your main XAML and navigate your other page to that frame. Your main remains visible AND active.

I do that already on bigger screens (Tablets) and know the pitfalls. Do not use the navigation helper for your other page, because you will not navigate back rather then send other "other pages" to that frame. Or remove last backstack entry after navigation.

secondFrame.Navigate(pageType, frames);
if (secondFrame.BackStackDepth > 0)
   secondFrame.BackStack.RemoveAt(secondFrame.BackStackDepth - 1);

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