1- using Prism . Commands ;
2- using Prism . Ioc ;
3- using Prism . Mvvm ;
1+ using System ;
2+ using System . IO ;
3+ using System . Linq ;
44using System . ComponentModel ;
55using System . Windows ;
6+ using System . Windows . Interop ;
7+
8+ using Prism . Commands ;
9+ using Prism . Ioc ;
10+ using Prism . Mvvm ;
11+ using Prism . Events ;
12+
613using XamlUtil . IO ;
14+ using XamlUtil . Common ;
715using XamlViewer . Models ;
816using XamlService . Commands ;
9- using System . Linq ;
10- using System . IO ;
11- using Prism . Events ;
1217using XamlService . Events ;
1318
1419namespace XamlViewer . ViewModels
@@ -18,9 +23,13 @@ public class MainViewModel : BindableBase
1823 private AppData _appData = null ;
1924 private IEventAggregator _eventAggregator = null ;
2025
26+ private HwndSource _hwndSource = null ;
27+ private HwndSourceHook _hwndSourceHook = null ;
2128 private GridLength _lastDataSourceColumnWidth = new GridLength ( 1 , GridUnitType . Star ) ;
2229
2330 public DelegateCommand ExpandOrCollapseCommand { get ; private set ; }
31+
32+ public DelegateCommand < Window > LoadedCommand { get ; private set ; }
2433 public DelegateCommand ActivatedCommand { get ; private set ; }
2534 public DelegateCommand < DragEventArgs > DropCommand { get ; private set ; }
2635 public DelegateCommand < CancelEventArgs > ClosingCommand { get ; private set ; }
@@ -47,14 +56,16 @@ private void InitEvent()
4756 private void InitCommand ( )
4857 {
4958 ExpandOrCollapseCommand = new DelegateCommand ( ExpandOrCollapse ) ;
59+
60+ LoadedCommand = new DelegateCommand < Window > ( Loaded ) ;
5061 ActivatedCommand = new DelegateCommand ( Activated ) ;
5162 DropCommand = new DelegateCommand < DragEventArgs > ( Drop ) ;
5263 ClosingCommand = new DelegateCommand < CancelEventArgs > ( Closing ) ;
5364 }
5465
5566 private void InitStatus ( )
5667 {
57- if ( _appData . Config . IsOpenDataSource )
68+ if ( _appData . Config . IsOpenDataSource )
5869 OnOpenDataSource ( true ) ;
5970 }
6071
@@ -92,6 +103,17 @@ private void ExpandOrCollapse()
92103 ExpandOrCollapse ( _isExpandSetting ) ;
93104 }
94105
106+ private void Loaded ( Window win )
107+ {
108+ if ( _hwndSourceHook == null )
109+ _hwndSourceHook = new HwndSourceHook ( WndProc ) ;
110+
111+ _hwndSource ? . Dispose ( ) ;
112+
113+ _hwndSource = PresentationSource . FromVisual ( win ) as HwndSource ;
114+ _hwndSource ? . AddHook ( _hwndSourceHook ) ;
115+ }
116+
95117 private void Activated ( )
96118 {
97119 if ( _appCommands == null )
@@ -117,16 +139,17 @@ private async void Closing(CancelEventArgs e)
117139 await _appData . DealExistedFileAction ? . Invoke ( ) ;
118140
119141 var dataSourceFile = ResourcesMap . LocationDic [ Location . DataSourceFile ] ;
120-
121- if ( ! string . IsNullOrWhiteSpace ( _appData . Config . DataSourceJsonString ) )
142+
143+ if ( ! string . IsNullOrWhiteSpace ( _appData . Config . DataSourceJsonString ) )
122144 FileHelper . SaveToFile ( dataSourceFile , _appData . Config . DataSourceJsonString ) ;
123- else
124- {
125- if ( FileHelper . Exists ( dataSourceFile ) )
126- File . Delete ( dataSourceFile ) ;
127- }
128-
145+ else
146+ {
147+ if ( FileHelper . Exists ( dataSourceFile ) )
148+ File . Delete ( dataSourceFile ) ;
149+ }
150+
129151 FileHelper . SaveToJsonFile ( ResourcesMap . LocationDic [ Location . GlobalConfigFile ] , _appData . Config ) ;
152+ _hwndSource ? . RemoveHook ( _hwndSourceHook ) ;
130153 }
131154
132155 #endregion
@@ -187,6 +210,20 @@ private void ExpandOrCollapse(bool isExpand)
187210 SettingRowHeight = isExpand ? GridLength . Auto : new GridLength ( 0 ) ;
188211 }
189212
213+ private IntPtr WndProc ( IntPtr hwnd , int msg , IntPtr wParam , IntPtr lParam , ref bool handled )
214+ {
215+ if ( msg == Win32 . WM_COPYDATA )
216+ {
217+ var cds = ( Win32 . CopyData ) System . Runtime . InteropServices . Marshal . PtrToStructure ( lParam , typeof ( Win32 . CopyData ) ) ;
218+ var xamlFiles = cds . lpData . Split ( "|" . ToCharArray ( ) , StringSplitOptions . RemoveEmptyEntries ) . Select ( f => Path . GetFullPath ( f ) ) . ToArray ( ) ;
219+
220+ _eventAggregator . GetEvent < OpenFilesEvent > ( ) . Publish ( xamlFiles ) ;
221+
222+ }
223+
224+ return hwnd ;
225+ }
226+
190227 #endregion
191228 }
192229}
0 commit comments