Question

Hi I am writing an app for windows phone 8 using vb and xaml. I have got all the fundimentals down but wish to store some data on the phone so that it is not lost when the application is reset. I have developed a number guessing game, I wish to store the users level and their coin balance on the phone then retrieve it when the application starts back up. I have found some references online how to do this in C# but nothing on vb. Could you please help?

Was it helpful?

Solution

If you only want to store a few values, you should make use of the IsolatedStorageSettings class. It will allow you to easily store key-value pairs in the Isolated Storage.

Sample VB.NET code taken from MSDN (link):

Imports System.IO.IsolatedStorage

Partial Public Class Page
    Inherits UserControl
    Private userSettings As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings

    Public Sub New()
        InitializeComponent()
        ' Retrieve and set user name.
        Try
            Dim name As String = CType(userSettings("name"), String)
            tbGreeting.Text = "Hello, " & name
        Catch ex As System.Collections.Generic.KeyNotFoundException
            ' No preference is saved.
            tbGreeting.Text = "Hello, World"
        End Try
    End Sub

    Private Sub btnAddName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Try
            userSettings.Add("name", tbName.Text)
            tbResults.Text = "Name saved. Refresh page to see changes."
        Catch ex As ArgumentException
            tbResults.Text = ex.Message
        End Try
    End Sub

    Private Sub btnChangeName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        userSettings("name") = tbName.Text
        tbResults.Text = "Name changed. Refresh page to see changes."
    End Sub

    Private Sub btnRemoveName_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If userSettings.Remove("name") = True Then
            tbResults.Text = "Name removed. Refresh page to see changes."
        Else
            tbResults.Text = "Name could not be removed. Key does not exist."
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        userSettings.Clear()
        tbResults.Text = "Settings cleared. Refresh page to see changes."
    End Sub

End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top