How can I manage different screens on a Windows Phone 7 application? - c#

For example, I want a button to take me to a settings page, and then back to the main screen. Just as an example for me to understand the workflow. Any guidance?

I use the NavigationService to navigate to the new page, where the Uri has a path relative to the project's base directory.
private void OptionsMenuItem_Click(object sender, EventArgs e)
{
// Navigate to the new page
NavigationService.Navigate(new Uri("/Views/OptionsView.xaml", UriKind.Relative));
}
The back button on the phone will take the user back to the previous page automatically, or you could code you own return button using the NavigationService again.

Dr. Herbie's method works great.
Another option is to implement INavigate on your PhoneApplicationPage. Then use a HyperlinkButton. If you have a lot of buttons and don't want to write a bunch of click handlers, this can be more convenient.
Your implementation of INavigate.Navigate just uses the page's NavigationService like this:
public bool Navigate(Uri source)
{
NavigationService.Navigate(source);
return true;
}

You must take care of certification requirements! Look at this tutorial: http://www.yourwindowsphone7.com/tutorials/navigation-in-windows-phone-7-apps.html

Related

Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another

In my Xamarin.Forms Prism app, I am using a MasterDetailPage for navigation.
While I am on one detail page, I would like to navigate to another detail page, as if I had selected it from the master page.
Initial navigation in App.xaml.cs:
protected override void OnInitialized()
{
...
NavigationService.NavigateAsync("MainPage/RootNavigation/MyFirstPage");
}
When I click a shortcut button on MyFirstPage, I would like to go to MainPage/RootNavigation/MySecondPage. The closest that I have been able to achieve has been using an absolute Uri.
private async void OnShortcutTapped(MyModel sender)
{
...
await _navigationService.NavigateAsync(new Uri("http://myapp.com/MainPage/RootNavigation/MySecondPage", UriKind.Absolute), navigationParams, null, false);
}
This basically gets me what I want, but after navigating in this manner, if I make the Master visible and select the menu item for MySecondPage, it refreshes the detail page as if it is navigating to the page.
Is there a better way to maintain this navigation, so that the master page knows that MySecondPage is already being displayed and it doesn't try to reload it?
While your navigation pattern doesn't make a lot of sense to me, you can achieve what you want by invoking a navigate command in the MasterDetailPageViewModel. You have a number of ways to do this. You could use the IEventAggregator to send a message to the MasterDetailPageViewModel to navigate, or you can use a CompositeCommand that invokes a DelegateCommand that exists on the MasterDetailPageViewModel.
You can see a sample of using a CompositeCommand here: https://github.com/PrismLibrary/Prism-Samples-Forms/tree/master/UsingCompositeCommands
You can also see how to send messages in this sample that I gave at the Xamarin Evolve conference: https://github.com/brianlagunas/Evolve2016SamplesAndSlides
Another option would be to just call a navigate command off the App.Current.MainPage ViewModel from with your MyFirstPage code behind.

Passing values from one page to another on a Windows phone project

I am trying to work out how to transfer string value from one windows phone app page to the next, following this article but I get an error that reads "The name 'NavigationService' does not exist in the current context".
There is a textbox and a button on page1. There is only a TEXTBLOCK on page 2. I wish to enter a string in the textbox of page 1, and when the button is clicked, the page 2 shows up with the text from the textbox (of page 1) in the textblock of page 2.
For the button click even in page 1, I entered this:
private void Button_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Page2.xaml?msg=" + Texbox_page1.Text, UriKind.Relative));
}
In the OnNavigatedTo method, in page 2, I entered
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("msg", out msg))
{
Textblock_nickname_display.Text = msg;
}
}
What am I missing? If there is n easier way to do this, please share!
I've spend almost 4 hours trying to figure this out, watching YouTube videos and what not!
Thanks in advance!
Make sure you have
using System.Windows.Navigation;
Also, make sure your pages inherit from the proper base class.
public class SecondPage : PhoneApplicationPage
{
...
}
EDIT:
Right click your project in the solution explorer and click Add > Reference and search for PresentationFramework. Adding that reference should give you the namespace you need.
Your project is Windows Phone 8.1 (Universal), So NavigationService isn't exist any longer.
You can use Frame.Navigate method in WP 8.1.
And below is my answer about Page Navigation in Windows Phone 8.1, please read it:
Windows Phone 8.1 - Page Navigation

How to cache a page in Windows Phone 8.1

Previously in Windows Phone 8.0 apps, we could navigate deeper to the same page this way:
NavigationService.Navigate(new Uri("/SamePage.xaml", UriKind.Relative));
Page was cached automatically, so after navigating back, user was on the same position on the list when he left.
But in Windows Phone Store Apps we navigate deeper to the same page this way:
Frame.Navigate(typeof(SamePage), id);
But after navigating back it loads data again so if user was on the middle of a long list, now he is on the top:
private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// TODO: Create an appropriate data model for your problem domain to replace the sample data.
var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
this.DefaultViewModel["Group"] = group;
}
How can I cache a page like the way it was done previously so user will be on the same position on a list where he left?
(I included Windows apps too cause they are familiar with it from a longer time).
In your page constructor you'll have to specify
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
In App.cs you can set RootFrame.CacheSize which hints the OS how many pages it should try to keep in cache.
Also you probably shouldn't reset the datacontext in NavigationHelper_LoadState - this method is called each time you navigate to the page, even if you navigate back.

Windows 8 Store App navigation with binding?

My first C#/Xaml-experience was Windows Phone 8 where navigation worked like this:
NavigationService.Navigate(new Uri("/MyPage.xaml", UriKind.Relative));
Which meant that I could replace the String "/MyPage.xaml" by anything I needed and go to that page.
Is something similar possible with Windows Store Apps?
this.Frame.Navigate(typeof(MyPage), UriKind.Relative);
MyPage isn't a string here so I can't simply replace it while the app is running. Still any way to do this?
What I am doing is:
I use a ListBox that gets it's data from a bound viewModel. There I wanted to store the target where the app should navigate to after a SelectionChanged Event is fired.
You can use it like below if you get the string with namespace.
this.Frame.Navigate(Type.GetType("Namespace.PageName"));
Yes you can pass the url of the page on selection changed and load the corresponding dataContext
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var title = NavigationContext.QueryString["title"];
(DataContext as ImagePageViewModel).Load(title);
}
Follow this example which would fulfill your requirement
page-navigation-in-windows-phone-and-windows-8

Windows Phone Page Navigation

I am working on a Windows Phone application, here is the scenario that I have problem:
So I have three pages, lets call it page 1, 2, 3.
In page 1, I have a button called start downloading. Click the button and use NavigateService.Navigate(page2Uri) and navigate to page2.
Page 2 makes query and downloads images from internet, so in its OnNavigateTo handler, I check the page back stack, if it is navigated from page 1, I will do the download. In the app bar of this page, I have a button that can navigate to page3.
Page 3 is a list of options that will perform some behavior on the image that is downloaded in page2. Once I choose an option, I want to go back to page 2 and perform some behavior on the loaded image.Here the question comes: if I use NavigateService.Navigate(page2Uri) to navigate from page3 to page2, it will call the Page2 constructor and OnNavigateTo handler again, which will cause it to lose every instance variable it already got.
But if I use NavigatService.GoBack it will go back to page2, then realizes that the backstack top entry is page1 (since page1 -> page2 -> page3). So it will re-download everything again.
I dont want anything to be downloaded again when navigate back form page3 to page2. So wondering if anyone has good idea about this.
Thank you.
You can use the query parameters and NavigationEventArgs to help.
First, you can use the NavigationEventArgs to determine if the user is going forward or background by checking the NavigationMode.
Second, you can tell page 2 to download by using the query parameters.
From page1:
private void MoveToPage2FromPage1()
{
NavigationService.Navigate(new Uri("/Page2.xaml?shouldDownload=true", UriKind.Relative));
}
and page2:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back) return;
string shouldDownload = ""; //May not be needed if you'll only ever go to page 2 from page 1 to download...
if (NavigationContext.QueryString.TryGetValue("shouldDownload", out shouldDownload))
{
Convert.ToBoolean(shouldDownload);
}
}
There are several ways to pass data to another page:
You can use query parameters as Shawn suggested.
You can use global data stored somewhere like in app.cs
You can use a static class to hold the data.
You can use a shared viewModel to hold the parameters. (or static properties in the viewmodel)
It all depends on the particular case. I think Shawns suggestion of using query paramaters is probably the most 'correct' MVVM way, but the other methods have their place.
You need to implement the following function and the navigation service.
These code will definitely solve your problem
for two or more parameters, use this code
String download="true";
String file="image";
NavigationService.Navigate(new Uri("/Page3.xaml?download="+download+"&file="+file+"", UriKind.Relative));
OnNavigatedTo, add the following code on to your Page2
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
String download=NavigationContext.QueryString["download"];
String file=NavigationContext.QueryString["file"];
}
For the above OnNavigatedTo function outputs true and image. You can use MessageBox.Show(); to output

Categories

Resources