Data transfer from page to page

I am looking for best practices for transferring data from page to page.

On page A, I have a button that fires. Page B.
On page B, I have 6 text fields that allow the user to enter information. When the user is done, click the button that returns them to page A.

I want to transfer this data to page A.

I saw the sentences:

  • create XML documents and save in isolated storage
  • use the App class to store information in properties
  • pass it as a query string

I am looking for the best practice. Is there one that Microsoft recommends, or one that is generally considered the best?

thank

+44
windows-phone-7
Feb 10 2018-11-11T00:
source share
8 answers
PhoneApplicationService.Current.State["yourparam"] = param NavigationService.Navigate(new Uri("/view/Page.xaml", UriKind.Relative)); 

then on another page just

 var k = PhoneApplicationService.Current.State["yourparam"]; 
+63
May 12 '11 at 15:47
source share

Personally, I would save the values โ€‹โ€‹entered on page B in the model (object), which is also available for page A.

Depending on how you go to page A a second time, one or more of the following actions may be helpful to help understand the values โ€‹โ€‹passed between pages:

How to transfer image value on one xaml page to another xaml page in Windows Phone 7?

Passing a complex object to a page while navigating in a WP7 Silverlight application

How to transfer object from xaml page to another?

How to pass value between Silverlight pages for WP7?

How to go from one xaml page to another and pass values?

+23
Feb 10 2018-11-11T00:
source share

One thing you can consider is to use MVC: let your application be the controller, save all the data in the model, and the pages are just views containing pure UI logic. In this case, your pages are artists, and you pass the model object. This provides a good isolation of the business logic and user interface so that you can easily translate them.

BTW, Silverlight and XAML are great tools for MVC, so this is a natural coincidence.

+4
Feb 10 '11 at 4:52
source share

There are a couple of things. First of all, if / when the user uses the "Back" button to return to page "A" instead of your button, the information is exchanged or not in the text box ("Back = Cancel" or "Back = OK?")

However, if you use NavigationService.GoBack (which you should use instead of NavigationService.Navigate, because if you use the Navigate call, repeated reverse key actions will cause all kinds of bad UX for your users), then QueryStrings are not an option. Since the pages really do not have the ability to link to each other in the WP7 Silverlight navigation system, you need to use a third-party organization to store your data. To do this, you can turn to (a) Isolated storage (slow and heavy, but fault tolerant), (b) Use the PhoneApplicationService.State dictionary, or (c) use some kind of global properties, either drop the object from the application, or using Statics / Singletons ...

Remember to monitor the behavior of Tombstoning when you do this - your page processes the OnNavigatedTo method when (a) you enter it in your application (b) you return to it when you finish your work on page B, or (c) you will tombstone your application from this page and return to your application using the "Back" key.

Sorry, I did not give a more direct answer there - a lot depends on your specific circumstances. In the most general case, I highly recommend using the application dictionary for applications in PhoneApplicationService ... it is a lightweight, easy to use and surviving tombstone. Just make sure your keys are unique as they should be.

+3
Feb 10 '11 at 5:01
source share

If you create a new Windows Phone project and use the Windows Phone Binding Template , most of the work you do will be done.

What you want to do is set up the ViewModel to contain all the data for your application. You can serialize and deserialize this data with IsolStorage so that it is saved in application sessions and during Tombstoning.

In the template, you will see MailViewModel and ItemViewModel . MainViewModel stores all the data your application needs, including the ObservableCollection ItemViewModel, and ItemViewModel is a separate data type for your application.

On the DetailsPage.xaml page, you'll want the DataBind of each text field in the App.MainViewModel elements. Set the binding to TwoWay if you want the ViewModel to be updated as soon as the user manipulates the data on DetailsPage.xaml. You can optionally bind to OneWay, and then click OK, which writes the changes back to the ViewModel and stores the IsolStorage value.

Here is an example of what the binding looks like:

 <TextBlock x:Name="ListTitle" Text="{Binding LineOne}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 

In this case, LineOne is a property in the ItemViewModel, and the page retrieves this data from the query string when the user selects an item from MainPage.xaml. The DataContext for the page determines where the data information comes from.

Here is a snippet in which MainPage passes the selected item from the ViewModel to DetailsPage.

 // Handle selection changed on ListBox private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { // If selected index is -1 (no selection) do nothing if (MainListBox.SelectedIndex == -1) return; // Navigate to the new page NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative)); // Reset selected index to -1 (no selection) MainListBox.SelectedIndex = -1; } 

This is how ItemPage gets the selected item.

 protected override void OnNavigatedTo(NavigationEventArgs e) { string selectedIndex = ""; if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex)) { int index = int.Parse(selectedIndex); DataContext = App.ViewModel.Items[index]; } } 

Play with the default template above and ask additional questions.

The beauty of data binding and ObservableCollection is that you can simply update the data, and the UX will reflect these changes immediately. This is because any data changes raise an event:

 public string LineOne { get { return _lineOne; } set { if (value != _lineOne) { _lineOne = value; NotifyPropertyChanged("LineOne"); } } } 

NotifyPropertyChanged (), which passes this information to the view.

+2
Feb 10 '11 at 16:01
source share

You can also keep it simple and use PhoneApplicationService.Current.State, which is basically a hash table. You will need to implement your own sorting in and out of isolated storage if you need to survive something.

Omar's suggestion to use the Windows Phone Binding Pattern is probably the best idea on this page. This is equivalent to my suggestion, but you will get a better result (more convenient code) due to a steeper learning curve.

I suggest you do it your own way, and then repeat the path of Omar.

+1
May 10 '11 at 9:43 a.m.
source share

how I implemented like this .. Is it right or not, I donโ€™t know ..

When you click on the news list page, it should open the news information page. I want to transfer the selected news content from the news feed to the news page.

The news list page contains the following method.

  protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e) { NewsDetailsPage newsDetailPage = (e.Content as NewsDetailsPage); if (newsDetailPage != null) newsDetailPage.SelectedNewsItem = SelectedNewsItem; //Contains the news details base.OnNavigatedFrom(e); } 

On the news page. U can access this object (SelectedNewsItem).

This may or may not be correct.

+1
Oct 12 '11 at 12:27
source share

One option is to use Application.Resources:

Save data:

 Application.Current.Resources.Add("NavigationParam", customers); NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative)); 

To get data:

 var customers = (List<Customer>) Application.Current.Resources["NavigationParam"]; 

Here the blog post is described in more detail: http://mikaelkoskinen.net/windows-phone-pass-data-between-pages-application-resources/ (author: me)

+1
Nov 19 '13 at 8:52
source share



All Articles