0

我是 wp7 开发的新手,尝试使用一个简单的示例,即..

我有一个图像控件和 2 个按钮,一个用于加载图像并将其显示在图像控件中,另一个用于将该图像保存到我需要创建的新文件夹中。

我有以下代码,我没有收到任何错误,但问题是我无法创建目录并将现有图像保存到该文件夹​​。

保存后我无法看到目录和图像。

我们能不能在模拟器中看到文件夹(或者)只能在windows phone上看到创建的目录?

Imports System.IO
Imports Microsoft.Phone.Tasks
Imports System.IO.IsolatedStorage
Imports System.Windows.Media.Imaging

Partial Public Class Page1
    Inherits PhoneApplicationPage

    Public Sub New()
        InitializeComponent()
        photoChooserTask = New PhotoChooserTask()
        AddHandler photoChooserTask.Completed, AddressOf photoChooserTask_Completed
    End Sub
    Dim photoChooserTask As PhotoChooserTask
    Private Sub photoChooserTask_Completed(sender As Object, e As PhotoResult)
        Dim bmp As System.Windows.Media.Imaging.BitmapImage = New System.Windows.Media.Imaging.BitmapImage()
        bmp.SetSource(e.ChosenPhoto)
        Image1.Source = bmp
        Dim originalFilename = Path.GetFileName(e.OriginalFileName)
        SaveImage(e.ChosenPhoto, originalFilename, 0, 100)

    End Sub
    Public Shared Sub SaveImage(ByVal imageStream As Stream, ByVal fileName As String, ByVal orientation As Integer, ByVal quality As Integer)
        Using isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()
            If isolatedStorage.FileExists("NewPics\fileName") Then
                isolatedStorage.DeleteFile("NewPics\fileName")
            End If

            If Not isolatedStorage.DirectoryExists("NewPics") Then
                isolatedStorage.CreateDirectory("NewPics")
            End If


            'isolatedStorage.CreateDirectory("NewPics")
            'Dim fileStream As New IsolatedStorageFileStream("fileName", FileMode.Create, isolatedStorage)
            Dim fileStream = isolatedStorage.CreateFile("NewPics\" + fileName)
            Dim bitmap = New BitmapImage()
            bitmap.SetSource(imageStream)

            Dim wb = New WriteableBitmap(bitmap)
            wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, orientation, quality)
            fileStream.Close()
        End Using
    End Sub
    Private Sub Button1_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Try
            photoChooserTask.Show()

        Catch ex As System.InvalidOperationException

            MessageBox.Show("An error occurred.")
        End Try
    End Sub
End Class

谁能告诉我我在哪里犯错?

4

1 回答 1

1

代码完美,问题是silverlight使用“隔离存储”来存储文件,顾名思义,就是一个完全隔离的存储。您的应用程序创建的文件或目录只能从您的应用程序访问。

我认为模拟器仅将隔离的存储文件存储在内存中,因为它不必在重新启动后保留它们。如果您想轻松查看应用程序的隔离存储内部,可以使用 Wp7 Explorer 之类的工具:http ://wp7explorer.codeplex.com/

于 2012-03-08T20:01:12.597 回答