使用 vb 为 Windows Phone 8 应用程序保存数据
Posted
技术标签:
【中文标题】使用 vb 为 Windows Phone 8 应用程序保存数据【英文标题】:Saving data for Windows Phone 8 app using vb 【发布时间】:2014-02-12 12:40:15 【问题描述】:您好,我正在使用 vb 和 xaml 为 windows phone 8 编写应用程序。我已经掌握了所有基础知识,但希望在手机上存储一些数据,以便在重置应用程序时不会丢失。我开发了一个猜数字游戏,我希望将用户级别和他们的硬币余额存储在手机上,然后在应用程序启动备份时检索它。我在网上找到了一些参考资料,如何在 C# 中执行此操作,但在 vb 上没有。你能帮忙吗?
【问题讨论】:
【参考方案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
【讨论】:
以上是关于使用 vb 为 Windows Phone 8 应用程序保存数据的主要内容,如果未能解决你的问题,请参考以下文章
StoreTest 工具包 Windows Phone 8.1
Windows Phone 8.1 中的 BackButtonPressed 问题?