Search code examples
c#uwpwindows-10-universalwindows-10-mobile

Windows 10 phone UWP lifecycle textBox


I have problem with saving and loading data from textBox. I want after put some data to textBox (when user fill textBox) and save them in onSuspending method and load them in OnResuming or OnLaunched method. I want to use LocalSettings.

The idea is that: user fill textBox and for example app crashed and when he will back all result are still in textBox.


Solution

  • You can make an event handler for each of your textboxes OnTextChanged() and add this code inside: ApplicationData.Current.RoamingSettings.Values["TextboxName"] = TextblockName.Text;

    When the user is finished with the form and you don't need to save the content of textboxes anymore then on the submit button you can add this code to clear the data ApplicationData.Current.RoamingSettings.Values["TextboxName"] = null; for each textbox.

    Now to get the data after app is crashed and user opens it again, you can simply add the following code in your Main() method for each of your textboxes:

    If(ApplicationData.Current.RoamingSettings.Values["TextboxName"] != null)
    {
       TextboxName.Text = (string)ApplicationData.Current.RoamingSettings.Values["TextboxName"];
    }