The user will fill out information in page 1, and the data will be seen in the other page listed in a listbox.
Is it advisable to use data binding?
I’ve been studying/researching about data binding already for about 2 days and still couldn’t understand it.
Simple example would really help.
As you may have understood by now, pages are stateless in wp7, which means that every time you go from one page to another, the state (variables, objects, etc.) of your previous page is lost.
A quick Google search provided me with this great blog article, and this slide show (see page 21 to 23, this is the second solution I list below).
In short, the author describes four solutions to pass data between pages:
- Using a Global Variable
- Using the Query String
- Using PhoneApplicationSerivce States
- Using NextPage instance
What should you use? Here is my advice regarding each solution:
- Unless the data is something that is shared among all pages of your
application, I suggest you forget about the Global Variable solution, it’s not
very clean. And even then, prefer solution 3. - The Query String solution is appropriate if you want to pass simple data between your pages, such as Strings, arithmetic values, Boolean, etc.
- About the PhoneApplicationService State, I never needed to use it personally but it looks cleaner than using a global variable. If you want to share some variable/object among all your pages, this is the way to go.
- The NextPage solution is the one you should use if you want to pass complex objects between two pages. It’s the solution I used the most because I did not need to pass data to all of my pages.
Regardless of the solution you choose, on the second page that is supposed to display data coming from the first page, you will have no trouble loading the data.
Finally, here is a link to a quick tutorial application where data is passed from page to page.
Let’s imagine that you have two pages: MainPage.xaml
and SecondPage.xaml
.
also you have two viewModels MainViewModel.cs
and SecondViewModel.cs
if you use some MVVM framework (MVVM light for example http://mvvmlight.codeplex.com/ ) you can use a ViewModelLocator, or you can write your own.
so, in OnNavigatied event in every page you can run this code:
var viewModel = ViewModelLocator.MainViewModel;
this.DataContext = viewModel;
Also you can do in a xaml. See the example here : http://jesseliberty.com/2011/01/04/wpfs-mvvm-light-toolkit-soup-to-nuts-part-i/
You can inject a data provider to every view model. So, when the user adds data on the MainPage
, MainViewModel
saves the data to the data provider (wich holds the data in a memory, or saves it to the isolated storage.)
Then, when you navigates to the SecondPage, SecondViewModel gets a data from the dataProvider
.
ViewModelLocator
should be responcible for Main and Second view models has the same DataProvider
instance.
If you need to pass any flag (was the data updated, Id, ect) you can do it by passing it in a Query String. ( http://jesseliberty.com/2011/01/09/passing-parameters-with-behaviors-in-mvvm-light-for-windows-phone/ )
This variant is good because your app can be thombstoned ( http://blogs.microsoft.co.il/blogs/arik/archive/2011/05/01/managing-tombstone-state-in-a-windows-phone-7-application.aspx) by the OS in any moment. You can handle this situation by sending a signal (executing a command, sending a message) to a current view model. And it will save data to storage. The
Page:1
PhoneApplicationService.Current.State["tittle"] = QTittle;
PhoneApplicationService.Current.State["name"] = QUsername;
NavigationService.Navigate(new Uri("/EditMyDetails.xaml", UriKind.RelativeOrAbsolute));
Page:2
var tittle = PhoneApplicationService.Current.State["tittle"];
var name = PhoneApplicationService.Current.State["name"];
1