parameter passing between XAML in windows phone - c#

There are 3 xaml pages, 2 of which call the third page with different parameters. How do I write onNavigatedTo() in the third page?
Am using NavigationService.Navigate(new Uri("/third.xaml?paramter=xxxx", UriKind.Relative));

Borrowed and adapted this code from http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626521(v=vs.105).aspx.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string msg = "";
if (NavigationContext.QueryString.TryGetValue("parameter", out msg))
// now your parameter is in the msg variable, and you could do stuff here.
}

Related

Passing data between multiple forms

I am creating a WindowsPhone app and (at the moment) I have three pages. Page1, page2 and page3. Page1 has two (2) textboxes that accepts the fist and last name, txtFN and txtLN and a button. The button takes you to the form2, that asks you the nickname. That has one textbox and a button. When the user clicks the button, it takes him the form3 that has a textblock. The nickname the user inputs in form2, displays in form3, on the textblock. I have gotten that part correct. Now, I am trying to do this: if the user does not input anything in the nick name section, I would like to show the first name in form three. Here's my code for form2->form3 event:
in form2:
private void Button_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(Page4_phone_Number_extraction), "Alright " + Texbox_nickname_extraction.Text + "!");
}
in form3,
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Textblock_nickname_display.Text = e.Parameter.ToString();
}
And this works like a charm..
Again, for the life of me, i can't think of how to have the first name show up in form3, if there is no user input.
One option would be to store the data in a local app settings like:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["FirstName"] = txtFN.txt;
in page 1.
You can then extract it in page 3 as:
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Textblock_nickname_display.Text=localSettings.Values["FirstName"].ToString();
You can also use OnNavigatedFrom() to achieve the same functionality, but it involves more work:
In Page1:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
Page2 destinationPage = e.Content as Page2;
if (destinationPage != null)
{
destinationPage.FirstName = txtFN.Text;
}
}
Make sure to have a string FirstName {get;set;} in Page2. Now use the same method to send the variable to Page3 too. You can then use the FirstName variable in Page3.
Needless to say, the first method is simpler.
Microsoft's documentation contains an example that shows that you shouldn't use OnNavigatedTo(NavigationEventArgs e) like you did but instead:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
string name = e.NavigationParameter as string;
if (!string.IsNullOrWhiteSpace(name))
{
tb1.Text = "Hello, " + name;
}
else
{
tb1.Text = "Name is required. Go back and enter a name.";
}
}

How to Work with NavigationService.Navigate in Windows Phone8.1

How To declare "?msg=" in WP8.1...?
i tried this code is working in WP8
In WP8
private void passParam_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));
}
coming to WP8.1 used Frame.Navigate
In WP8.1
private void passParam_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(SecondPage.xaml) + textBox1.Text);
}
Then how to declare "?msg=" in WP8.1....?
If you look at Frame.Navigate you will see, the second parameter is a navigation parameter you can pass. Take a look at http://mikaelkoskinen.net/winrt-xaml-navigating-from-page-to-page-how-it-differs-from-windows-phone-7/
In Windows Phone 8.1, Page Navigation method is:
Frame.Navigate(typeof(SecondPage), param);
or
Frame.Navigate(typeof(SecondPage));
In your code, you can set param to be yours like this:
Frame.Navigate(typeof(SecondPage), textBox1.Text);
You can find the documentation for this here MSDN
As people already wrote, what you need is:
Frame.Navigate(typeof(SecondPage), textBox1.Text);
The second paramater can be actually any object.
Then, to get the paremeter in SecondPage you can do this (in your case):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
String your_text = e.Parameter as String;
}

What typeof(Page) Am I Navigating From?

In the Basic Page template, there is the method NavigationHelper_LoadState(object sender, LoadStateEventArgs e){}. How can I check to see what the previous page was?
if(sender.GetType() == typeof(PreviousPage)){} does not work!
My primary objective is to know where my data is coming from in clean in code.
Although one option is to send a parameter with page type, I wouldn't do it because you might need that parameter to pass some other relevant data between pages.
So, I'd rather do something like this:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (Frame.CanGoBack)
{
PageStackEntry lastPage = Frame.BackStack[Frame.BackStackDepth - 1];
if (lastPage.SourcePageType == typeof(MainPage))
{
// do something
}
}
this.navigationHelper.OnNavigatedTo(e);
}
Your method won't work as NavigationHelper_LoadState(object sender, LoadStateEventArgs e){} is being called by NavigationHelper and the the sender passed is NavigationHelper - there is no information passed from which Page the navigation has started.
I don't see any properties in which you could read this information, but you can surely pass your previous type as a parameter:
// when navigating:
Frame.Navigate(typeof(Page1), this.ToString());
// in target Page:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter == typeof(MainPage).ToString())
Debug.WriteLine("Previous was MainPage");
}

How can I get textbox text from Blank Page to MainPage?

I want to get the text from blankpage.xaml to Mainpage.xaml
I try to use this on MainPage.xaml:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
enter.Text = (App.Current as App).trytext;
}
but there is an error that trytext could not found so i use and if statement:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (enter.Text == "")
{
enter.Text = "";
}
else
{
enter.Text = (App.Current as App).trytext;
}
}
But when I get the text from blankpage there is no text when I get back to mainpage
please help!
I'm not sure what you're trying to do, but it seems like you're trying to creating a variable that's accessible through the whole application?
If you want to pass some data from page 1 to page 2, you can pass the information in the Navigate method.
Probably you're calling something like
Frame.Navigate(typeof(MainPage));
The Navigate method has an extra parameter in which you can pass your info. Call it like Frame.Navigate(typeof(MainPage), enter.Text);
In the OnNavigatedTo you can get the value from the NavigationEventArgs.
probably something like:
enter.Text = e.Parameter.ToString();

Windows 8 navigation, passing an URI as parameter

I just started developing for windows 8, and I'm having some difficult in passing parameters in navigation between pages.
What I am trying to do is: I have two pages (page1 and page2) In page1, there's a menu with a button. When I click in this button, the click event should pass as a parameter an possible URI or path, that will set the source of a image in page2.
this is the code in page1:
private void Button_Click_2(object sender, RoutedEventArgs e)
{
//this.Frame.Navigate(typeof(SplitPage), "ms-appx:/Imgs/1.png");
this.Frame.Navigate(typeof(SplitPage), new Uri("ms-appx:/Imgs/1.png", UriKind.RelativeOrAbsolute));
}
and page2, for receiving the Uri:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var imgSource = e.Parameter as ImageSource;
this.imgParaPintar.Source = imgSource;
}
I'm noticing that imgSource is receiving nothing, it keeps as null.
So, any clues on what I'm doing wrong or what I am missing?
I think you need code like this (untested):
protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.imgParaPintar.Source = new BitmapImage((Uri)e.Parameter);
}
You might need to add using Windows.UI.Xaml.Media.Imaging; to your file, if you don't have it there already.
It's worth noting, as stated in the comments here:
http://answers.flyppdevportal.com/categories/metro/csharpvb.aspx?ID=4b847d71-9cd5-4457-add9-f68e457b23ff
That you can only pass primitive types as navigation parameters.

Categories

Resources