Vb.net 不确定如何使用IsolatedStorage

Vb.net 不确定如何使用IsolatedStorage,vb.net,visual-studio,windows-phone-7,Vb.net,Visual Studio,Windows Phone 7,我想使用Visual Basic为Windows Phone 7创建notes应用程序。我读过一些教程,但它们都适合C而不是VB。基本上,我有一个主页和一个添加注释的页面。用户在“添加注释”页面上键入注释后,该注释的标题将显示在主页面上。我还希望用户能够选择该标题,它将显示注释。我做了一些研究,我知道我需要使用隔离存储,不知道如何在VB中实现它来保存笔记。我想我还需要一个列表框来存储笔记的标题。我不是要求别人给我代码,我是要求一些关于VB的教程,或者任何关于实现这个的指针或一般帮助。谢谢MSDN

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

MSDN上的所有代码示例都有C和VB版本。看

“常用应用程序开发任务”下的“模型视图”“视图”“模型”示例可能是一个很好的起点。
下载VB代码的链接是

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

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

从这里可以读到很多东西

开始之前,需要将IsolatedStorage导入到项目中

Imports System.IO
Imports System.IO.IsolatedStorage
创建文件夹

这将在应用程序独立存储中创建一个目录。它将被称为NewFolder

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("NewFolder\SomeFile.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("NewFolder\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
Else
    writeFile = New StreamWriter(New IsolatedStorageFileStream("NewFolder\SomeFile.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
保存和读取文本文件

要使用内容保存文本文件,您可以执行以下操作:

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("NewFolder\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
Else
    writeFile = New StreamWriter(New IsolatedStorageFileStream("NewFolder\SomeFile.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独立存储的一些基本知识以及如何使用它,但我强烈建议您在