別のスレッド内からポップアップを表示するのに問題があります。ここで他の質問をいくつか読んでみましたが、それらをどのように適用できるか理解できません...
カメラを使用するアプリがあり、利用可能になった後に画像のプレビューをポップアップ表示したいと考えています。
PhotoCamera にはイベント 'CaptureImageAvailable' があり、そこでプレビューをポップアップ表示したい
private void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
BitmapImage img = new BitmapImage();
img.SetSource(e.ImageStream);
e.ImageStream.Close();
//creating the new pop-up preview
Grid panelPreview = new Grid();
panelPreview.Background = new SolidColorBrush(Colors.Black);
panelPreview.Width = 300;
panelPreview.Height = 400;
//create an ImageBrush to display the image in the new pop-up
ImageBrush imgPreview = new ImageBrush();
imgPreview.ImageSource = img;
//need a rectangle to add the image to, otherwise can't add it
Rectangle rect = new Rectangle();
rect.Width = 300;
rect.Height = 400;
rect.Fill = imgPreview;
//Adding the rectangle to the grid
panelPreview.Children.Add(rect);
imagePreview.IsOpen = true;
....
}
これにより、UnauthorizedAcessException が発生します。これは、このイベントが別のスレッドにあるため、私には理にかなっていますが、理解できないのは、それを修正する方法です...このコードも使用してみました
private void cam_CaptureImageAvailable(object sender, ContentReadyEventArgs e)
{
Deployment.Current.Dispatcher.BeginInvoke(delegate()
{
BitmapImage img = new BitmapImage();
img.SetSource(e.ImageStream);
e.ImageStream.Close();
//creating the new pop-up preview
Grid panelPreview = new Grid();
panelPreview.Background = new SolidColorBrush(Colors.Black);
panelPreview.Width = 300;
panelPreview.Height = 400;
//create an ImageBrush to display the image in the new pop-up
ImageBrush imgPreview = new ImageBrush();
imgPreview.ImageSource = img;
//need a rectangle to add the image to, otherwise can't add it
Rectangle rect = new Rectangle();
rect.Width = 300;
rect.Height = 400;
rect.Fill = imgPreview;
//Adding the rectangle to the grid
panelPreview.Children.Add(rect);
imagePreview.IsOpen = true;
});
このメソッドはエラーをスローしませんが、実際には何もしていないようです。このポップアップを UI スレッドに表示するにはどうすればよいですか?