图像控件不要在绑定更改时更改源

本文关键字:绑定 控件 图像 | 更新日期: 2023-09-27 18:12:57

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

in my 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;
}

My XAML

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

当扫描多个图像时,窗口中的图像不会改变,直到所有扫描完成,即使我在TwainWin_TwainTransfer中设置了一个断点,它在扫描的每个图像和RaisePropertyChanged中都被调用,当扫描完成时,它只显示最后一个图像

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

类TwainLib (twainInterface.Acquire(true);调用)中scan的代码

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);在一个任务但相同的结果,

图像控件不要在绑定更改时更改源

我把

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

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