Laire
Laire

Reputation: 1330

UWP frame in frame navigation - How to access the parent frame?

in my UWP app I have the situation, that I use two frames.

For example the main page has a frame (MainFrame) and load in this frame a subpage, this has also a frame (SubFrame1) and load a third page in this frame.

Now I want to change the the content of the MainFrame with click on a button on the third page.

When I use:

this.Frame.Navigate(typeof(FourthPage));

It loads the FourthPage in the frame from the subpage (SubFrame1) but I want to load the FourthPage in the MainFrame.

Upvotes: 4

Views: 4881

Answers (2)

Kibria
Kibria

Reputation: 141

Main Home page have this:

<SplitView>
    <SplitView.Pane>
    </SplitView.Pane>
    <SplitView.Content>
        <Frame Name="MainFrame"></Frame>
    </SplitView.Content>
</SplitView>

Now another page is navigated within this "MainFrame":

MainFrame.Navigate(typeof(AnotherPage));

Meaning "MainFrame" is now the parent frame and within it, another frame is allocated.

Now if we want to navigate this parent frame staying in the child frame, then we can access it in the following way:

Frame parentFrame = Window.Current.Content as Frame;
parentFrame.Navigate(typeof(MainPage));

Upvotes: 2

Grace Feng
Grace Feng

Reputation: 16652

Just for example here, if your third page is like this:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Button Content="Navi from parent" Click="Button_Click" />
</Grid>

Then in the button click event you can find your MainFrame like this:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var grid = this.Frame.Parent as Grid;
    var page = grid.Parent as SubFramePage;
    var mainframe = page.Parent as Frame;
    mainframe.Navigate(typeof(FourthPage));
}

It's just a sample, maybe your layout is not like so, point is that you can find the parent layer-by-layer.

Upvotes: 3

Related Questions