Windows Phone 7用のアプリケーションを最初から最後まで作成します。 パート15. Windows Phoneの写真へのアクセス

前の部分



写真やその他の画像をアプリケーションリソースとして使用して、ユーザーインターフェイスに表示できます。 アプリケーションは、携帯電話のメディアライブラリ内の写真にアクセスしたり、ユーザーが内蔵カメラで写真を撮ることを許可したりできます。



このパートでは、次のことを学習します。

Windows Phoneで写真にアクセスする



メディアライブラリの画像とカメラの画像にアクセスするには、PhotoChooserTaskとCameraCaptureTaskを使用します。 これらのクラスは、ユーザーが選択した別のウィンドウを開いてアプリケーションに返されるため、選択(選択)と呼ばれます。 別のウィンドウを開くランチャー (起動)もありますが、ユーザーの選択をアプリケーションに返しません。



PhotoChooserTaskとCameraCaptureTaskには同様のコードが必要なので、PhotoChooserTaskについてのみ説明します。 さらに、選択した写真を分離ストレージに保存する方法についても説明します。 XNA MediaLibraryクラスを使用して、画像を受け取ってメディアライブラリに保存することもできます。 ただし、このクラスは考慮されません。



チューザーを使用するには、インスタンス化する必要があります。 次の画像は、PhotoChooserTaskの例を示しています。



画像



ユーザーが選択を行ったら、Completedイベントを処理して適切なアクションを実行する必要があります。 次のコードスニペット(CarDetailsPage.xaml.csから)は、PhotoChooserTaskを作成し、そのCompletedイベントを処理する方法を示しています。 PhotoChooserTaskはページのグローバル変数でなければならず、Completedイベントはコンストラクターで有効にする必要があることに注意してください。

private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  1. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  2. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  3. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  4. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  5. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  6. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  7. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  8. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  9. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  10. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  11. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  12. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  13. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  14. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  15. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  16. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  17. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  18. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  19. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  20. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  21. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  22. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  23. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  24. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  25. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  26. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



  27. private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .



private readonly PhotoChooserTask photoTask = new PhotoChooserTask(); public CarDetailsPage() { InitializeComponent(); photoTask.Completed += PhotoTask_Completed; // ... other code ... } private void PhotoButton_Click( object sender, RoutedEventArgs e) { photoTask.Show(); } private void PhotoTask_Completed( object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { var image = new System.Windows.Media.Imaging.BitmapImage(); image.SetSource(e.ChosenPhoto); CarDataStore.SaveTempCarPhoto(image, delegate { MessageBox.Show( "There is not enough space on " + "your phone to save your selection. Free some " + "space and try again." , "Warning" , MessageBoxButton.OK); }); car.Picture = image; hasUnsavedChanges = true ; } } * This source code was highlighted with Source Code Highlighter .





ユーザーが[写真]ボタンをクリックすると、PhotoChooserTaskが表示されます。 これにより、アプリケーションが非アクティブ化されます。 ユーザーが写真を選択すると、アプリケーションが再アクティブ化されますが、この場合、OnNavigatedToメソッドが呼び出されて再アクティブ化を処理する前にPhotoChooserTask.Completedイベントが発生します。



Completedイベントハンドラーは、新しいBitmapImageを作成し、選択した写真をソースとして設定します。 次に、イベントハンドラーはイメージを分離ストレージに配置します。 イメージはシリアル化できないため、メモリに保存されている状態辞書にも、分離ストレージの状態辞書にも保存できません。 したがって、イメージを保存する場合は、分離ストレージ仮想ファイルシステムインターフェイスを使用して保存する必要があります。 CarDataStoreクラスは、ユーザーが[保存]ボタンをクリックした後、写真を分離されたストレージに配置する必要もあります。 したがって、一時的な写真を処理するためのSaveTempCarPhotoおよびGetTempCarPhotoメソッドは、保存された写真の処理に使用されるSaveCarPhotoおよびGetCarPhotoメソッドを単純にラップします。 SaveCarPhotoメソッドとGetCarPhotoメソッドについては、「分離ストレージ:データの保存とロード」セクションで説明しました。



最後に、イベントハンドラーは、 carフィールドに格納されているCarオブジェクトのPictureプロパティに画像を割り当てます。 その後、写真はPictureプロパティに関連付けられたImageコントロールに表示されます。



注:

Completedイベントハンドラーに渡されるPhotoResultクラスには、OriginalFileNameプロパティが含まれています。 このプロパティは、選択した写真をXNA MediaLibraryから取得するために使用すると便利で、分離したストレージに個別に保存する必要はありません。 ただし、執筆時点では、OriginalFileNameの実装は期待どおりに動作していません。一時ファイル名を返すため、後でXNA MediaLibraryクラスから写真を取得するのに役立ちません。 ただし、この方法でメディアライブラリを使用できる場合でも、ユーザーが選択した画像を分離ストレージに保存することをお勧めします。 この場合、ユーザーが後でメディアライブラリから画像を削除したり、他のアプリケーションを使用してコンテンツを変更したりしても、アプリケーションで使用できます。



All Articles