locked
LocalSettings is empty after 12 hours

    Question

  • Hello,

    I'm having trouble using the LocalSettings container. If my application is closed during a certain time (more than 12 hours), the LocalSettings container is empty.

    I create the container using this:

    ApplicationData.Current.LocalSettings.CreateContainer("mylocalsetting", ApplicationDataCreateDisposition.Always);

    So I guess the LocalSettings container is destroyed in some ways, and when I access my data, it is created with empty value.

    Do you have any idea why my container is destroyed? Is there a way to avoid that? Should I use an other container?

    Thanks.

    Tuesday, November 4, 2014 9:01 AM

Answers

  • So, I think you start the project with blank project template , and use blank page template.

    you can add a new item by basic page template, the visual studio will ask if you want to add another necessary compoments, click "yes", it will add SuspensionManager.cs , NavigationHelper.cs and so on.

    Then, you  can modiy your  OnSuspending method to

           private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                await SuspensionManager.SaveAsync();
                deferral.Complete();
            }

    PS: I would like to create project by other templates (eX: hub template), then use basic page template to create main page , modify app.xaml.cs to navigate to my MainPage first. this way will automatically create some useful code for us.


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。


    • Edited by Bill ChungMVP Tuesday, November 4, 2014 10:27 AM
    • Marked as answer by BatRocchia Thursday, November 6, 2014 3:52 PM
    Tuesday, November 4, 2014 10:26 AM

All replies

  • Check your App.xaml..cs

    1. in Public App(), is there "this.Suspending += this.OnSuspending;" ?

    2. in protected async override void OnLaunched(LaunchActivatedEventArgs e),

    is there " SuspensionManager.RegisterFrame(rootFrame, "AppFrame");" ?

    3. in  private async void OnSuspending(object sender, SuspendingEventArgs e),

    is there " await SuspensionManager.SaveAsync();"  ?


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。


    Tuesday, November 4, 2014 9:29 AM
  • My App.xaml.cs has this code:

    /// <summary>
    /// Invoked when application execution is being suspended.  Application state is saved
    /// without knowing whether the application will be terminated or resumed with the contents
    /// of memory still intact.
    /// </summary>
    /// <param name="sender">The source of the suspend request.</param>
    /// <param name="e">Details about the suspend request.</param>
    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
       var deferral = e.SuspendingOperation.GetDeferral();
       //TODO: Save application state and stop any background activity
       deferral.Complete();
    }

    There are no SuspensionManager available. Where can I find the source code?

    Tuesday, November 4, 2014 9:41 AM
  • So, I think you start the project with blank project template , and use blank page template.

    you can add a new item by basic page template, the visual studio will ask if you want to add another necessary compoments, click "yes", it will add SuspensionManager.cs , NavigationHelper.cs and so on.

    Then, you  can modiy your  OnSuspending method to

           private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                await SuspensionManager.SaveAsync();
                deferral.Complete();
            }

    PS: I would like to create project by other templates (eX: hub template), then use basic page template to create main page , modify app.xaml.cs to navigate to my MainPage first. this way will automatically create some useful code for us.


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。


    • Edited by Bill ChungMVP Tuesday, November 4, 2014 10:27 AM
    • Marked as answer by BatRocchia Thursday, November 6, 2014 3:52 PM
    Tuesday, November 4, 2014 10:26 AM