绑定更改时,图像控件不会更改源

时间:2013-05-10 14:37:55

标签: c# wpf binding mvvm-light

我在代码中使用事件来通知图像是否被扫描

在我的ViewModel中:

public BitmapImage ImageSelected
{
   get{return _imageSelected;}
   set
   {
        if (_imageSelected == value)
        {
            return;
        }
        _imageSelected = value;
        RaisePropertyChanged(() => ImageSelected);
   }
}

public ICommand CmdScan
{
   get
   {
       return new DelegateCommand(Scan);
   }
}
WpfTwain twainInterface = null;
private BitmapImage _imageSelected = null;

private void Scan()
{

    twainInterface = new WpfTwain();
    twainInterface.TwainTransfer += new TwainTransferReadyHandler(TwainWin_TwainTransfer);
    twainInterface.Acquire(true);
}


private void TwainWin_TwainTransfer(BitmapImage imageSources)
{
    ImageSelected = imageSources;
}

在我的XAML中

<Image Margin="151,12,356,12" Stretch="Fill" Source="{Binding ImageSelected}" />

当扫描多个图像时,窗口中的图像不会改变,直到所有扫描完成,即使我在TwainWin_TwainTransfer中创建了一个断点,并且在扫描的evry图像和RaisePropertyChanged中也调用了它,并且当扫描完成时它只显示最后一张图片

欢迎任何建议,我是MVVM的新手

更新

在TwainLib类中扫描的代码(由twainInterface.Acquire(true);调用)

do
{
   pxfr.Count = 0;
   hbitmap = IntPtr.Zero;

   TwImageInfo  iinf = new TwImageInfo();
   rc = DSiinf( appid, srcds, TwDG.Image, TwDAT.ImageInfo, TwMSG.Get, iinf );
   if( rc != TwRC.Success )
   {
      CloseSrc();
      return pics;
   }

   rc = DSixfer( appid, srcds, TwDG.Image, TwDAT.ImageNativeXfer, TwMSG.Get, ref hbitmap );
   if( rc != TwRC.XferDone )
   {
      CloseSrc();
      return pics;
   }

   rc = DSpxfer( appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.EndXfer, pxfr );
   if( rc != TwRC.Success )
   {
      CloseSrc();
      return pics;
   }
   pics.Add(hbitmap);
   BitmapImage bitmapImage = /*converting hbitmap to BitmapImage  */
   TwainTransferReady(bitmapImage); 
}
while( pxfr.Count != 0 );

我也尝试在任务中获得twainInterface.Acquire(true);但结果相同,

1 个答案:

答案 0 :(得分:0)

我替换了

private void TwainWin_TwainTransfer(BitmapImage imageSources)
{
ImageSelected = imageSources;
}

App.Current.MainWindow.Dispatcher.Invoke(DispatcherPriority.Input, new ThreadStart(() => ImageSelected = imageSources));