mvvmuwpwindows-template-studio

Databinding during Navigation in UWP


I'm using Windows Template Studio to create a simple UWP app. I'm using the built-in MVVM template for Multiple Pages. I have 2 Pages on the app. Both the pages contain the few textboxes where the user can enter some values. Data binding is working perfectly on both the pages individually. My problem is: When I navigate to Page 2 and return back on Page 1, the value in the Textboxes return to the original initialization values and do not remain the last updated values.

I am expecting that when I return to Page 1 from Page 2, the values on Page 1 should be the last updated values. I've followed a lot of tutorials but could not find a comprehensive solution to it. I am just getting started with UWP and MVVM so it may be hard for me to wrap my head around some of the basic concepts. Any help in this regard is appreciated.


Solution

  • You need to think about saving these TextBox values in the ApplicationData. For example, the ApplicationData.LocalSettings.

    When you navigate from page1 to page2, you could save the value in localSettings.

    var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    localSettings.Values["exampleSetting"] = "Hello Windows";
    

    When you get back from page2 to page1. You could get the value from LocalSettings.

    Object value = localSettings.Values["exampleSetting"];
    

    Please see the Application data sample for more details.