using Cognex.VisionPro.ToolBlock; using Cognex.VisionPro; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using LampInspectionMachine.Views; using Prism.Commands; using System.Windows; using LampInspectionMachine.Core; using Prism.Events; using Prism.Ioc; using Prism.Regions; using Prism.Mvvm; using System.Configuration; using System.Drawing.Design; using System.Windows.Forms.Integration; using System.Threading; using System.Windows.Interop; using LampInspectionMachine.Model; using System.Globalization; namespace LampInspectionMachine.ViewModels { public class CameraViewModel : BindableBase, IConfirmNavigationRequest { IContainerProvider _container; private Management management; private int index; public Management Management { get => management; set { SetProperty(ref management, value); } } public CameraViewModel(IContainerProvider container, IRegionManager regionManager, IEventAggregator eventAggregator) { _container = container; Management = _container.Resolve(); } private DelegateCommand _SoftTriggerCommand; /// /// 开始采集 /// public DelegateCommand SoftTriggerCommand => _SoftTriggerCommand ?? ( _SoftTriggerCommand = new DelegateCommand(OnSoftTrigger) ); private DelegateCommand _SoftTrigger_ContinueCommand; /// /// 开始采集 /// public DelegateCommand SoftTrigger_ContinueCommand => _SoftTrigger_ContinueCommand ?? ( _SoftTrigger_ContinueCommand = new DelegateCommand(OnSoftTrigger__Continue) ); private DelegateCommand _CameraInifCommand; /// /// 相机初始化 /// public DelegateCommand CameraInifCommand => _CameraInifCommand ?? ( _CameraInifCommand = new DelegateCommand(OnCameraInit) ); private DelegateCommand _CameraCloseCommand; /// /// 相机关闭 /// public DelegateCommand CameraCloseCommand => _CameraCloseCommand ?? ( _CameraCloseCommand = new DelegateCommand(OnCameraClose) ); private void OnSoftTrigger() { Management.SoftTrigger(); } private void OnSoftTrigger__Continue() { Management.SoftTrigger__Continue(); } private void OnCameraInit() { Management.InitCamera(); } /// /// 接收参数 /// /// /// public void OnNavigatedTo(NavigationContext navigationContext) { if ( navigationContext.Parameters.ContainsKey("Index") ) { index= Convert.ToInt32(navigationContext.Parameters.GetValue("Index")); if ( Management.VisionProManagers!=null ) { if ( index < Management.VisionProManagers.Count ) { Management.CurrCamera = Management.VisionProManagers[ index ].Camera; Management.CurrCamConfig = Management.VisionProManagers[ index ].CameraInfo; Management.CurrCameraSn = Management.CurrCamConfig.SerialNumber; Management.Currvision=Management.VisionProManagers[ index ]; } } } } private void OnCameraClose() { Management.CloseCamera(); } public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { if ( Management.CurrCamConfig != null ) { Management.CurrCamConfig.SerialNumber = Management.CurrCameraSn; } continuationCallback(true); } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } } public static class CogDisplayBinder { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached( "ImageSource", typeof(ICogImage), typeof(CogDisplayBinder), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.None, OnImageSourceChanged)); public static ICogImage GetImageSource(WindowsFormsHost host) => ( ICogImage ) host.GetValue(ImageSourceProperty); public static void SetImageSource(WindowsFormsHost host, ICogImage value) => host.SetValue(ImageSourceProperty, value); public static void OnImageSourceChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { if ( d is WindowsFormsHost host && host.Child is CogRecordDisplay display ) { if ( e.NewValue is ICogImage newImage ) { // 跨线程安全更新 host.Dispatcher.Invoke(() => { try { display.Image = newImage; display.Fit(true); } catch { } }); } else { host.Dispatcher.Invoke(() => display.Image = null); } } } } }