locked
[WP8.1][VB]Using ApplicationData.Current.LocalSettings.Values To Save State In Windows Phone 8.1 RRS feed

  • Question

  • I have a Windows Phone 8.1 app in which I need to save state. I would like to do this using

    ApplicationData.Current.LocalSettings.Values

    However, this would require me to manually save the values every time I changed their values, which seems very inefficient. In other apps I have written, I created the variables in App.xaml.vb and saved/restored them in the OnSuspending & OnLaunched methods using XML. However, because the variables in this case are local (in MainPage.xaml.vb), this will not work. The only workaround I can come up with is to modify my code to have the variables in App.xaml.vb. Is there any way to use ApplicationData.Current.LocalSettings.Values without manually saving the values every time I change them? Thanks.


    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, August 11, 2016 8:06 PM

All replies

  • Hi Nathan Sokalski,

    Maybe we can use a Module to set and get the variable in it. Please check the following code to see if it can help:

    Public NotInheritable Class MainPage
        Inherits Page
    
        Private Sub BtnSetAppdata_Click(sender As Object, e As RoutedEventArgs)
            SampleStringSetting.Samplestring = "test string"
        End Sub
    
        Private Sub BtnGetAppdata_Click(sender As Object, e As RoutedEventArgs)
            Dim a As String
            a = SampleStringSetting.Samplestring
        End Sub
    End Class
    
    Public Module SampleStringSetting
        Public Property Samplestring
            Get
                Return ApplicationData.Current.LocalSettings.Values("samplestring").ToString()
            End Get
            Set(value)
                ApplicationData.Current.LocalSettings.Values("samplestring") = value
            End Set
        End Property
    End Module
    


    Best Regards, Sunteen Wu

    Friday, August 12, 2016 9:11 AM
  • I thought about doing it that way (using the Set/Get methods of a Property), but that would not work for arrays (when setting only one item) or objects (when setting a property of the object) because the Get method returns a reference to the array or object, so setting a specific item or property of an object would not trigger the Set method, therefore not updating ApplicationData.Current.LocalSettings.Values. This is why I wanted to use the OnSuspending & OnLaunched methods of App.xaml.vb, because they are always called when the app is launched or suspended. Any ideas? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Friday, August 12, 2016 8:32 PM
  • Hi Nathan Sokalski,

    Currently what ways you are using for change their values if you don't want to directly save them with APIs? What data(an array or something else) you are trying to save to the application data? Could you please give me a sample?

    With special data people may think out a way which is fit you.

    Why you want to use OnSuspending &OnLaunched  methods?  Did you need update the application only when suspending and launching?


    Best Regards, Sunteen Wu

    Monday, August 15, 2016 9:13 AM