在参考http://tizianocacioppolini.blogspot.com/2014/01/windows-phone-8-folders-and-files.html#.U6YbkfhOXIU我试图把一个样本放在一起,我可以使用 PhotoChooserTask 和使用 AppicationData 保存/检索这张照片。最终目标是使用新ApplicationData
方法将图像保存在文件夹中,如果该文件夹已存在,则将其删除并替换。我不确定如何正确设置
MainPage.xaml.cs
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
//If an image exists in the folder, set the image to the page background using TombstoningHelper.cs Retrieve method
}
private void Browse_Click(object sender, RoutedEventArgs e)
{
photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
// get the file stream and file name
Stream photoStream = e.ChosenPhoto;
string fileName = Path.GetFileName(e.OriginalFileName);
//Save the Image to storage using TombstoningHelper.cs
}
}
TombstoningHelper.cs
public async Task StorePhoto(Stream photoStream, string fileName)
{
StorageFolder folder;
//Check if folder exists
//If folder exists, delete it
// persist data into isolated storage
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream current = await file.OpenStreamForWriteAsync())
{
await photoStream.CopyToAsync(current);
}
}
public async Task<BitmapImage> RetrievePhoto(string fileName)
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
Stream imageStream = await file.OpenStreamForReadAsync();
//Check if file exists
// display the file as image
BitmapImage bi = new BitmapImage();
bi.SetSource(imageStream);
return bi;
}