0

How can I navigate to back frame in uwp app? I have a frame and it loads three pages in that frame. So when I click on back button I need to see the previous page loaded in that frame. Please help. In the code I need to get the page loaded in the Mainframe

        <Image Source="/Assets/Images/PURlogo_large.png" HorizontalAlignment="Left"  Margin="70,950" Width="212" Height="78"/>
            </Grid>
        </StackPanel>
        <Frame x:Name="MainFrame" Grid.Column="1" Content="{Binding FrameData,Mode=OneWay}" >
        </Frame>
        <Grid Background="Red" Visibility="Collapsed" x:Name="testgrid">
            <TextBlock Text="hello world"></TextBlock>
        </Grid>
    </Grid>

In App.xaml.cs : I have a common method to handle the back button.

private bool On_BackRequested()
     {
        Frame rootFrame = Window.Current.Content as Frame;


        if (rootFrame.Content is HomeView homeview)
        {
           // here I do nothing as it is handled by the back method in homeview.xaml.cs
        }

        if (rootFrame.Content is MyPageView myPage)
        {
            if (rootFrame.CanGoBack)
            {
                //Here its going back to the page instead of the frame.
            }

        }

        else if (rootFrame.CanGoBack)
        {
            rootFrame.GoBack();
            return true;
        }
        return false;

    }

this is in the homeview.xaml.cs where the frame is located. This works fine.

private void OnBackRequested(object sender, BackRequestedEventArgs e)
    {
        if (MainFrame.CanGoBack)
        {
            e.Handled = true;
            MainFrame.GoBack();
        }
    }

my navigation is like this. page1->page2(contains frame)->framepage 1->framepage2->page 3

Once I click back from page 3 instead of going to page 2 second frame page(framepage2) it goes to page 2 with first frame page. So how to fix that using a common method as the one above in app.xaml.cs

sadik
  • 107
  • 10
  • just call "MainFrame.GoBack()" whenever you want to go back in the frame stack. – Muhammad Touseef Oct 17 '18 at 09:22
  • I am getting a run time exception (System.Runtime.InteropServices.COMException: 'Error HRESULT E_FAIL has been returned from a call to a COM component.' )there. I have created a common method for the back button in App.xaml.cs and I am calling this method from there. – sadik Oct 17 '18 at 09:43
  • why do u need a common method? technically you can just call "Frame.GoBack" from within any page, you dont even need to mention name of the frame because each page has its parent frame attached to it called "this.Frame" – Muhammad Touseef Oct 17 '18 at 09:58
  • I am implementing this back functionality for the whole app. I have other two pages and this page where the frames are loaded. In this page I have many pages loaded in the frame tag and need to go back on frames – sadik Oct 17 '18 at 10:08
  • https://learn.microsoft.com/en-us/windows/uwp/design/basics/navigation-history-and-backwards-navigation – Muhammad Touseef Oct 17 '18 at 11:08
  • How can I implement this frame back navigation if I wanted to implement it as part of global method as per the document. – sadik Oct 17 '18 at 11:45
  • if (MainFrame.CanGoBack) { MainFrame.GoBack(); } this will never return true because looks like you made navigation through different way i need to check your code, i had the similar issue but i resolve and found what a mistake i am doing while making whole navigation using frames – Shubham Sahu Oct 17 '18 at 12:47
  • I am using binding to navigate to frame. Any other way to navigate to the frame? – sadik Oct 23 '18 at 09:03
  • @ShubhamSahu I have used MainFrame.Navigate and the back navigation in frames is working fine.I have another issue though, I have a frame which navigates to a page and while clicking back its going back to the page where the frame is loaded instead of the frame. Anyway to fix that? Thanks – sadik Oct 30 '18 at 12:39
  • yes i am definitely talking about that issue ;), you should get back to previous frame but you are navigated to very first frame. Right ? i only want to see your c# codes with current xaml – Shubham Sahu Oct 30 '18 at 12:42
  • alternately share your solution (with all other app secrets and code deleted only inc. only your frame navigation things), you are doing something wrong e.g SubFrames like thing. – Shubham Sahu Oct 30 '18 at 12:50
  • 1
    @ShubhamSahuI have updated my code. Thanks in advance – sadik Oct 31 '18 at 06:40
  • @sadik due to out from PC unable to answer, you can download sample from below link in answer – Shubham Sahu Nov 04 '18 at 17:38

1 Answers1

0

XAML SecondPage

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Button x:Name="FrameBackButton" Grid.Column="0" Content="Back" Height="55" Width="255" Margin="5" Click="FrameBackButton_Click"/>
        <Button x:Name="Frame2Button" Grid.Column="1" Content="Load Frame2" Height="55" Width="255" Margin="5" Click="Frame2Button_Click"/>
        <Button x:Name="Frame3Button" Grid.Column="2" Content="Load Frame3" Height="55" Width="255" Margin="5" Click="Frame3Button_Click"/>
    </Grid>

    <Frame x:Name="MainFrame" Grid.Row="1"/>
</Grid>

C# Second Page

public sealed partial class SecondPage : Page
{
    public SecondPage()
    {
        this.InitializeComponent();
        MainFrame.Navigated += MainFrame_Navigated;
        this.Loaded += SecondPage_Loaded;

    }

    private void MainFrame_Navigated(object sender, NavigationEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        foreach (var item in rootFrame.BackStack.ToList())
        rootFrame.BackStack.Remove(item);
    }

    private void SecondPage_Loaded(object sender, RoutedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;
        frame.BackStack.RemoveAt(frame.BackStackDepth - 1);
        MainFrame.Navigate(typeof(Frame1));
    }        

    private void Frame2Button_Click(object sender, RoutedEventArgs e)
    {
        if (MainFrame.CurrentSourcePageType != typeof(Frame2))
        {
            MainFrame.Navigate(typeof(Frame2));
        }
    }

    private void Frame3Button_Click(object sender, RoutedEventArgs e)
    {
        if (MainFrame.CurrentSourcePageType != typeof(Frame3))
        {
            MainFrame.Navigate(typeof(Frame3));
        }
    }

    private void FrameBackButton_Click(object sender, RoutedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;

        if (MainFrame.Content is Frame1)
        {
            frame.Navigate(typeof(MainPage));
        }
        else
        {                
            if (MainFrame.CanGoBack)
                MainFrame.GoBack();
        }
    }

}

Sample

Shubham Sahu
  • 1,963
  • 1
  • 17
  • 34
  • Can I navigate to a frame from a page using back button. That is what confusing me? It doesn't navigate back to last loaded frame instead goes to the default frame. – sadik Nov 06 '18 at 11:37
  • @sadik Have you checked the sample – Shubham Sahu Nov 06 '18 at 16:20
  • @Shubam Sahu I did and it works, but I do navigate to some other pages from page 2 and that doesn't contain a frame and once I click back from page 3 I should be navigating to page 2 with frame 3 but instead I am going to page 2 with frame 1. How can I fix this? – sadik Nov 09 '18 at 09:06
  • @sadik you should disclosed everything you need so i can manage codes accordingly, but you have manage backbutton from page 3 to go back to page 2 – Shubham Sahu Nov 09 '18 at 14:07