代码之家  ›  专栏  ›  技术社区  ›  Alexander Stolz

WPF中图像更新的TargetInvocationException

  •  3
  • Alexander Stolz  · 技术社区  · 15 年前

    我已经构建了一个显示图像的WPF控件。现在我想以非常快的速度更改图像。 我已经构建了一个ImageContainer类,该类保存图像,并具有一个ChangedEventHandler,它在更改时更新我控件中的图像。

    执行的代码如下:

    videoImageThread = new Thread(
                new ThreadStart(
                  delegate()
                  {
                      this.VideoCapture.Dispatcher.Invoke(
                        System.Windows.Threading.DispatcherPriority.Normal,
                        new Action(
                          delegate()
                          {
    
                              videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;
    
                          }
                      ));
                  }
              ));
    
    
    private void Instance_VideoRefresh()
        {
            if (VideoImageContainer.Instance.VideoImage != null)
            {
                lock (videoImageSetLock)
                {
                    videoImageThread.Start();
                }
            }
        }
    

    此代码引发System.Reflection.TargetInvocationException,我做错了什么?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Markus Hütter    15 年前

    在我看来,你是在调用一个线程来调用一个线程?!

    您是否尝试直接调用调度程序上的操作,如下所示:

    private void Instance_VideoRefresh()
    {
        if (VideoImageContainer.Instance.VideoImage != null)
            this.VideoCapture.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(
                      delegate()
                      {
                          videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;
                      }
                  ));
    }
    
        2
  •  0
  •   micahtan    15 年前

    是否尝试简单地将videoimage.source绑定到属性,并在实例的videorefresh方法中更改该属性?

    我以前尝试过使用image/list<imagesource>/timer组合,它工作得很好。