Passing the data of a certain page to another page. Windows Phone 7

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:

  1. Using a Global Variable
  2. Using the Query String
  3. Using PhoneApplicationSerivce States
  4. Using NextPage instance

What should you use? Here is my advice regarding each solution:

  1. 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.
  2. The Query String solution is appropriate if you want to pass simple data between your pages, such as Strings, arithmetic values, Boolean, etc.
  3. 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.
  4. 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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var viewModel = ViewModelLocator.MainViewModel;
this.DataContext = viewModel;
</code>
<code>var viewModel = ViewModelLocator.MainViewModel; this.DataContext = viewModel; </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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> PhoneApplicationService.Current.State["tittle"] = QTittle;
PhoneApplicationService.Current.State["name"] = QUsername;
NavigationService.Navigate(new Uri("/EditMyDetails.xaml", UriKind.RelativeOrAbsolute));
</code>
<code> PhoneApplicationService.Current.State["tittle"] = QTittle; PhoneApplicationService.Current.State["name"] = QUsername; NavigationService.Navigate(new Uri("/EditMyDetails.xaml", UriKind.RelativeOrAbsolute)); </code>
 PhoneApplicationService.Current.State["tittle"] = QTittle;
            PhoneApplicationService.Current.State["name"] = QUsername;


        NavigationService.Navigate(new Uri("/EditMyDetails.xaml", UriKind.RelativeOrAbsolute));

Page:2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> var tittle = PhoneApplicationService.Current.State["tittle"];
var name = PhoneApplicationService.Current.State["name"];
</code>
<code> var tittle = PhoneApplicationService.Current.State["tittle"]; var name = PhoneApplicationService.Current.State["name"]; </code>
 var tittle = PhoneApplicationService.Current.State["tittle"];

        var name = PhoneApplicationService.Current.State["name"];

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật