1

別のスレッド内からポップアップを表示するのに問題があります。ここで他の質問をいくつか読んでみましたが、それらをどのように適用できるか理解できません...

カメラを使用するアプリがあり、利用可能になった後に画像のプレビューをポップアップ表示したいと考えています。

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 スレッドに表示するにはどうすればよいですか?

4

2 に答える 2

0

このようにディスパッチャを使用できます

Deployment.Current.Dispatcher.BeginInvoke(()=>{

      something...

});
于 2012-10-18T09:23:42.163 に答える
0

「画像プレビュー」とは?
何らかの種類のポップアップまたは画像要素コンテナーである場合は、「panelPreview」を子として追加して、UI に表示されるようにする必要があります。

もう1つ、サムネイル画像を使用できます(

private void cam_CaptureThumbnailAvailable(オブジェクト送信者, ContentReadyEventArgs e)

)ポップアップで表示するため、実際の画像よりもメモリが少なくて済みます。

于 2012-10-18T12:29:27.690 に答える