ntwain/samples/Sample.WPF/MainWindow.xaml.cs

106 lines
3.1 KiB
C#
Raw Normal View History

2014-04-21 04:57:38 +08:00
using GalaSoft.MvvmLight.Messaging;
using ModernWPF.Controls;
using ModernWPF.Messages;
2014-04-21 04:57:38 +08:00
using NTwain;
using NTwain.Data;
using System;
2014-11-15 11:52:07 +08:00
using System.Collections;
2014-04-21 04:57:38 +08:00
using System.ComponentModel;
using System.Diagnostics;
2014-04-03 07:01:21 +08:00
using System.Linq;
using System.Reflection;
2014-04-03 07:01:21 +08:00
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
2014-04-21 04:57:38 +08:00
using System.Windows.Media.Imaging;
2014-04-03 07:01:21 +08:00
2015-03-16 05:36:17 +08:00
namespace Sample.WPF
2014-04-03 07:01:21 +08:00
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
TwainVM _twainVM;
2014-04-03 07:01:21 +08:00
public MainWindow()
{
InitializeComponent();
2014-04-12 23:22:05 +08:00
if (!DesignerProperties.GetIsInDesignMode(this))
{
_twainVM = this.DataContext as TwainVM;
2014-09-25 09:52:28 +08:00
2015-07-04 23:18:03 +08:00
Messenger.Default.Register<RefreshCommandsMessage>(this, m => m.HandleIt());
Messenger.Default.Register<MessageBoxMessage>(this, msg =>
2014-04-12 23:22:05 +08:00
{
2014-04-15 07:30:25 +08:00
if (Dispatcher.CheckAccess())
{
2015-07-04 23:18:03 +08:00
msg.HandleWithModern(this);
2014-04-15 07:30:25 +08:00
}
else
{
Dispatcher.BeginInvoke(new Action(() =>
{
2015-07-04 23:18:03 +08:00
msg.HandleWithModern(this);
2014-04-15 07:30:25 +08:00
}));
}
2014-04-12 23:22:05 +08:00
});
}
2014-04-03 07:01:21 +08:00
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = _twainVM.State > 4;
base.OnClosing(e);
}
protected override void OnClosed(EventArgs e)
{
_twainVM.CloseDown();
base.OnClosed(e);
}
2014-04-03 07:01:21 +08:00
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_twainVM.WindowHandle = new WindowInteropHelper(this).Handle;
2014-04-03 07:01:21 +08:00
}
2014-04-08 07:46:03 +08:00
private void CapList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var capVM = CapList.SelectedItem as CapVM;
if (capVM != null)
{
2014-11-15 11:52:07 +08:00
CapDetailList.ItemsSource = capVM.Get();
CapDetailList.SelectedItem = capVM.GetCurrent();
2014-04-03 07:01:21 +08:00
}
else
{
CapDetailList.ItemsSource = null;
2014-04-03 07:01:21 +08:00
}
}
private void CapDetailList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
2014-11-15 11:52:07 +08:00
var capVM = CapList.SelectedItem as CapVM;
if (capVM != null)
{
if (capVM.Supports.HasFlag(QuerySupports.Set))
{
try
{
capVM.Set(CapDetailList.SelectedItem);
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
{
ex = ex.InnerException;
}
2014-11-15 11:52:07 +08:00
ModernMessageBox.Show(this, ex.Message, "Cannot Set", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
2014-04-03 07:01:21 +08:00
}
}
}