使用vb保存Windows Phone 8应用程序的数据

时间:2014-02-12 12:40:16

标签: vb.net xaml windows-phone-8 load save

您好我正在使用vb和xaml为Windows Phone 8编写应用程序。我已经完成了所有的基础工作,但希望在手机上存储一些数据,以便在重置应用程序时不会丢失。我开发了一个数字猜谜游戏,我希望在手机上存储用户级别和硬币余额,然后在应用程序启动时检索它。我在网上找到了一些如何在C#中执行此操作但在vb上没有任何内容。你能帮忙吗?

1 个答案:

答案 0 :(得分:1)

如果您只想存储一些值,则应使用IsolatedStorageSettings类。它允许您轻松地将键值对存储在隔离存储中。

从MSDN(link)获取的示例VB.NET代码:

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