不确定如何使用IsolatedStorage



我想使用Visual Basic为Windows Phone 7创建一个notes应用程序。我读了一些教程,但它们都适合C#而不是VB。基本上,我有一个主页和一个添加注释的页面。一旦用户在添加注释页面上键入注释,该注释的标题就会显示在主页面上。我还希望用户能够选择该标题,它将显示注释。我已经做了一些研究,我知道我需要使用隔离存储(不知道如何在VB中实现它)来保存笔记。我想我还需要一个列表框来存储笔记的标题。我并不是要求别人只给我代码,我是要求在VB中提供一些关于这方面的教程,或者任何关于实现这一点的指针或一般帮助。感谢

MSDN上的所有代码示例都有C#和VB版本。请参阅http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx

模型视图ViewModel Sample(在"通用应用程序开发任务"下)可能是一个很好的起点
下载VB代码的链接是http://go.microsoft.com/fwlink/?LinkId=229339

如果您想将笔记写入手机(而不是某个地方的云),那么您需要使用IsolatedStorage,这是正确的。这里有一个链接,指向一个博客条目,该条目有一个类(在Visual Basic中),该类有一些辅助方法,这些方法将为您提供一些与VB.Net传统方法(如ReadAllText、WriteAllText)类似的方法。这可能是您想要的文件系统读/写(但至少可以让您开始使用隔离存储)。

http://www.blakepell.com/2012-03-07-wp7-file-helpers

独立存储

独立存储用于在Windows Phone上存储本地文件,如文本文件、图像、视频等。每个应用程序都被分配了一个特定的独立存储,并且仅限于该应用程序。没有其他应用程序能够访问您的应用程序隔离存储。

可以从这里阅读很多内容关于Windows Phone独立存储的所有内容

在开始之前,您需要将IsolatedStorage导入到您的项目中。

Imports System.IO
Imports System.IO.IsolatedStorage

创建文件夹

这将在应用程序隔离存储中创建一个目录。它将被称为"新文件夹"

Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
myIsolatedStorage.CreateDirectory("NewFolder")

您可以在文件夹内的文件夹中创建文件夹,如下所示:

myIsolatedStorage.CreateDirectory("NewFolder/NewFolder1/NewFolder2/NewFolder3")

删除文件夹:

myIsolatedStorage.DeleteDirectory("NewFolder")

创建和删除文件夹时的一个好做法是在文件夹创建方法周围添加一个Try Catch语句,这样,如果出现异常,您或用户将被通知发生异常的原因,例如,不存在的文件夹因此无法删除,或者存在的文件夹需要替换等。下面的示例显示了使用Try Catch创建文件夹的基本功能陈述

Public Sub CreateDirectory(directoryName As String)
Try
    Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    If Not String.IsNullOrEmpty(directoryName) AndAlso Not myIsolatedStorage.DirectoryExists(directoryName) Then
        myIsolatedStorage.CreateDirectory(directoryName)
    End If
        ' handle the exception
Catch ex As Exception
End Try
End Sub

要使用它,你可以做:

Me.CreateDirectory("NewFolder")

对于删除方法:

Public Sub DeleteDirectory(directoryName As String)
Try
    Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    If Not String.IsNullOrEmpty(directoryName) AndAlso myIsolatedStorage.DirectoryExists(directoryName) Then
        myIsolatedStorage.DeleteDirectory(directoryName)
    End If
        ' handle the exception
Catch ex As Exception
End Try
End Sub

使用它:

Me.DeleteDirectory("NewFolder")

创建文件

你可以创建一个像这样的空文件:

Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim writeFile As New StreamWriter(New IsolatedStorageFileStream("NewFolderSomeFile.txt", FileMode.CreateNew, myIsolatedStorage))

删除文件:

myIsolatedStorage.DeleteFile("NewFolder/SomeFile.txt")

和以前一样,在创建文件时,一个好的做法是始终检查您正在写入或删除的目录是否存在。

你可以做一些类似的事情:

Try
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim writeFile As StreamWriter
If Not myIsolatedStorage.DirectoryExists("NewFolder") Then
    myIsolatedStorage.CreateDirectory("NewFolder")
    writeFile = New StreamWriter(New IsolatedStorageFileStream("NewFolderSomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
Else
    writeFile = New StreamWriter(New IsolatedStorageFileStream("NewFolderSomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
End If
    ' do something with exception
Catch ex As Exception
End Try

保存和读取文本文件

要将文本文件与您的内容一起保存,您可以执行以下操作:

Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Using writeFile As New StreamWriter(New IsolatedStorageFileStream("myNote.txt", FileMode.Create, FileAccess.Write, myIsolatedStorage))
Dim someTextData As String = "This is some text data to be saved in a new text file in the IsolatedStorage!"
writeFile.WriteLine("note data")
writeFile.Close()
End Using

要读取文本文件的内容:

Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim fileStream As IsolatedStorageFileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read)
Using reader As New StreamReader(fileStream)
TextBlock.Text = reader.ReadLine()
End Using

我已经为您提供了Windows Phone独立存储的一些基础知识以及如何使用它,但我强烈建议您在此处

上阅读更多关于它的信息

最新更新