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;
}