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.
Related
I'm making Windows universal app using Visual Studio 2013. I'm trying to accomplish something like in this article.
On the main page I have many pictures. After clicking on one of these pictures the user goes to another page, which has a check box.
After user checkes the check box, I have to change the image on main page.
I don't know if this code is correct, but I'm doing something like this for second page
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
points.Text = (Convert.ToInt32(points.Text) + 4).ToString();
var currentCheckBoxItem = sender as CheckBox;
if (currentCheckBoxItem.IsChecked == true)
{
otra.bedre1.Source = (new Uri("ms-appx:///Assets/complete.png", UriKind.Absolute));
// }
But this is code for mainpage where can choose task:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
bedre1.Source = ImageSource;
}
Any ideas?
You would need to store the name and Checkstatus as fields inside SQlite database so that its available even after the app is closed. So you would need to do some research on it.
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;
}
I am using App.RootFrame.GoBack(); but it does not refresh the page (which makes sense).
I would like to enforce a page refresh... Anyway of doing this?
Possible duplicate of this.
In any event, you probably want to use the NavigationService as follows:
if(NavigationService.CanGoBack())
{
NavigationService.GoBack();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// ensure view has the data it needs
}
This seemed to do the job well.
IEnumerable<JournalEntry> pages = App.RootFrame.BackStack;
string pageURL = pages.First().Source.OriginalString;
App.RootFrame.Navigate(new Uri(pageURL + "?Refresh=true&random=" + Guid.NewGuid(), UriKind.RelativeOrAbsolute));
on a 'GoBack()' the OnNavigatedTo() method will be run, but the constructor for the page won't be. Therefore you need to refresh the page yourself in the OnNavigatedTo() method.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Refresh the data in here
}
Also I would suspect that you would be better of using the navigationservice to go back i.e:
NavigationService.GoBack();
Basically I have a longlistselector and it contains writings when one of them is selected I am going to navigate to a page and its going to show the all detailed information of this writing (article , date , image etc.)
Don't worry about the data, I checked and data contains the details all I need is how to send this Writing object (named data) with navigating.Or can you give any suggestion how can I do this but without using uri query
Thank you
private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
Writing data = selector.SelectedItem as Writing;
NavigationService.Navigate(new Uri("/WritingPage.xaml", UriKind.Relative));
}
To pass an Object from one page to another PhoneApplicationservice is easiest way. Here is the simple example to pass Object from one page to another. Its tested.
private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (PhoneApplicationService.Current.State.ContainsKey("Data"))
if (PhoneApplicationService.Current.State["Data"] != null)
PhoneApplicationService.Current.State.Remove("Data");
LongListSelector selector = sender as LongListSelector;
Writing data = selector.SelectedItem as Writing;
PhoneApplicationService.Current.State["Data"] = data ;
NavigationService.Navigate(new Uri("/WritingPage.xaml", UriKind.Relative));
}
//On second page
//I assume you want to Data on page load
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Writing data = new Writing ();
data =(Writing)PhoneApplicationService.Current.State["Data"]
PhoneApplicationService.Current.State.Remove("Data");
}
Are you coding in MVVM ?
If yes, you can send and register to an Event (published by your ViewModel linked to the LongList and subscribe this event in your ViewModel linked to WrittingPage.)
Simple and better way is to make "Writing" object scope Global.
Here, I added a class file to solution "Global.cs" and I declared "data" variable of "Writing" type.
public static Writing data = new Writing();
Use the same code you did and access the "data" variable here. I have done some minor change to your code.
private void longList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LongListSelector selector = sender as LongListSelector;
Global.data = selector.SelectedItem as Writing;
NavigationService.Navigate(new Uri("/WritingPage.xaml", UriKind.Relative));
}
Now access "data" variable on "WritingPage.xaml" after you navigate to it. Use value of this variable and then make it null.
In my app, I have a page where I navigate from the MainPage.xaml. A CameraCaptureTask is launched from that page.
I would like to know, on page loading, how can I know if I have navigated from MainPage.xaml, or it came back from CameraCaptureTask.
Using the NavigationService.BackStack.FirstOrDefault() doesn't help, because, as expected, it returns the MainPage.xaml.
I'd like something like:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (/*Navigated-from-MainPage-but-not-from-CameraCaptureTask*/)
{
// Do Something
}
}
You can accomplish this in one of two ways. The first is to listen to the Completed event of the CameraCaptureTask
CameraCaptureTask task = new CameraCaptureTask();
task.Completed += new EventHandler<PhotoResult>(OnCaptureTaskCompleted);
task.Show();
...
void OnCaptureTaskCompleted(object sender, PhotoResult e)
{
// Navigated back or took a picture
}
The second way is to check the NavigationMode of the NavigationEventArgs. If the value is Back, then you know you navigated back from the CameraCaptureTask (assuming that this page does not navigate to any other pages)
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
// came back from CameraCaptureTask
}
}