C# Coding

Tom Bradburn

New member
Dec 12, 2013
25
0
0
Visit site
Looked through all the MSDN tutorials to find how to navigate to another page and use the back button to go back to the last page, however, when testing it didn't do anything. Not even the code I wanted only the XAML code.
 

startrunner

New member
Jan 21, 2014
24
0
0
Visit site
Unlike WPF/Windows Desktop apps, in Windows Phone you cannot create and show your own windows so you are given one and expected to put all of your functionality in it. Window.Current is a reference to the window given by the OS. The empty project template includes a piece of code located in App.xaml.cs that sets a frame as the content of the app's window on startup. The reference to that frame is called rootFrame and it is supposed to be the host for all your application pages and views. A Frame has a method Navigate([PageType]) that takes the CLR type of your page, constructs a new object of that type and displays it into its body. You can get the current view hosted in the frame with the rootFrame.Content property and set its DataContext, but you cannot change the value of the property itself because you are supposed to use the Navigate([Page type]) method.
In order to handle the Back Button, you need to subscribe to the HardwareButtons.BackPressed event and in the handler set e.Hanled=true after having it navigate to the previous page. If you don't do so, your application will be terminated.
Here is an example usage:
Code:
var rootFrame=Window.Current.Content=new Frame();
rootFrame.navigate(typeof(MainWindow));

(rootFrame.Content as Page).DataContext=new MainViewModel(new MainModel());

HardwareButtons.BackPressed=((sender, args)=>{/*TODO: Navigate backwards*/args.Handled=true;});
Have a look at these articles and references:
https://msdn.microsoft.com/en-us/library/windows.phone.ui.input.hardwarebuttons.backpressed.aspx
https://msdn.microsoft.com/en-us/library/system.windows.controls.frame(v=vs.110).aspx
The frame does have some implementation of navigation history and back-and-forward navigation but you don't necessarily have to use it.
 

Expression2

New member
Oct 2, 2013
79
0
0
Visit site
Well actually you can actually create more than one window in the Windows Runtime. I've forgotten the API since it's not really practical as the new UX design can fit everything in one window with pages and navigation and app bars.
 

Members online

Forum statistics

Threads
326,656
Messages
2,248,715
Members
428,533
Latest member
Eldonmcartney