我是wp7开发的新手,尝试使用一个简单的例子,即..
我有一个图像控件和2个按钮,一个用于加载图像并在图像控件中显示,另一个用于将图像保存到我需要创建的新文件夹中。
我有以下代码,我没有收到任何错误,但问题是我无法创建目录并将现有图像保存到该文件夹。
保存后我无法看到该目录以及图片。
我们可以在模拟器中看到该文件夹(或者)我们只能在Windows手机上看到创建的目录吗?
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
有谁能说我在哪里弄错了?
答案 0 :(得分:1)
代码是完美的,问题是silverlight使用“隔离存储”来存储文件,顾名思义,这是一个完全隔离的存储。您的应用程序创建的文件或目录只能从您的应用程序访问。
我认为模拟器只将隔离的存储文件存储在内存中,因为重启后不必保留它们。如果您想在应用程序的隔离存储中轻松查看,可以使用Wp7 Explorer等工具:http://wp7explorer.codeplex.com/