Best way to going back the navigation like a click in back button

Oct 31, 2013 at 12:11 PM
I want to going back the navigation of the main page. Exactly like a click in the back button. How can i make this?
Oct 31, 2013 at 3:41 PM
I needed something like this - the ability to have my main frame go back to the previously shown page. In other words, do exactly the same thing that pressing the back button would do, but do it in response to something else (i.e.: show a page, and when a selection is made on that page, simply return to the previous page)

What I did to accomplish this was to obtain the Content frame in my MainWindow.cs like so:
private ModernFrame f;
public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    f = (ModernFrame)GetTemplateChild("ContentFrame");
}
and also have my MainWindow register for a NavigateBackMessage that any one of my Page's viewmodels could send.
public MainWindow()
{
    InitializeComponent();
    Messenger.Default.Register<NavigateBackMessage>(this, m => ReceiveNavigateBackMessage(m));  
}
and upon receipt of the message I do:
 private void ReceiveNavigateBackMessage(NavigateBackMessage m)
{
    NavigationCommands.BrowseBack.Execute(null, f);
}
and that's it.
Marked as answer by latchuk on 10/31/2013 at 9:55 AM
Oct 31, 2013 at 4:09 PM
Where is the class Messenger used in this block of code?
public MainWindow()
{
    InitializeComponent();
    Messenger.Default.Register<NavigateBackMessage>(this, m => ReceiveNavigateBackMessage(m));  
}
Oct 31, 2013 at 4:13 PM
Where is the class Messenger used in the second block of code?
Oct 31, 2013 at 5:36 PM
Edited Oct 31, 2013 at 5:37 PM
The Messenger class is part of the MVVM framework that I am using (mvvmlight) that facilities the loosely coupled communication between my ViewModels, and to my other View components. It is not part of the MUI framework.

I'm not sure if you are using the MVVM pattern in your application, however just figure out a way to call a method in your MainWindow from wherever your code is that you require going back a page. For example, if you make the "ReceiveNavigateBackMessage" method public (and rename it to something more appropriate since you're not receive a message), then you can access it via:
var mw = Application.Current.MainWindow
mw.CallTheBackMethod();
Oct 31, 2013 at 5:47 PM
ok i understood.

I don't use the MVVM framework so I made my way. In the MainWindow:
Public Shared Property Frame As ModernFrame

    Public Overrides Sub OnApplyTemplate()
        MyBase.OnApplyTemplate()
        MainWindow.Frame = Me.GetTemplateChild("ContentFrame")
    End Sub
and in the click of button:
Private Sub BtnCancel_Click(sender As Object, e As RoutedEventArgs)
        NavigationCommands.BrowseBack.Execute(Nothing, MainWindow.Frame)
    End Sub
It's working now.

Thank you for your attention flyte!
Marked as answer by latchuk on 10/31/2013 at 9:55 AM
Nov 8, 2013 at 4:56 PM
Thanks for the code Latchuk.

I implemented your solution and it works for me.
Nov 18, 2013 at 5:24 PM
Edited Nov 18, 2013 at 11:25 PM
nevermind