1414using System . Windows . Media . Imaging ;
1515using System . Windows . Shapes ;
1616using AsyncAwaitBestPractices ;
17+ using AsyncAwaitBestPractices . MVVM ;
1718using IconPacks . Browser . Model ;
1819using IconPacks . Browser . Properties ;
1920using JetBrains . Annotations ;
2021using MahApps . Metro . Controls ;
21- using MahApps . Metro . Controls . Dialogs ;
2222using MahApps . Metro . IconPacks ;
2323using Microsoft . Win32 ;
2424using IO = System . IO ;
@@ -32,25 +32,22 @@ public class IconPackViewModel : ViewModelBase
3232 private ICollectionView _iconsCollectionView ;
3333 private string _filterText ;
3434 private IIconViewModel _selectedIcon ;
35- private readonly IDialogCoordinator dialogCoordinator ;
3635
37- private IconPackViewModel ( MainViewModel mainViewModel , IDialogCoordinator dialogCoordinator )
36+ private IconPackViewModel ( MainViewModel mainViewModel )
3837 {
3938 this . MainViewModel = mainViewModel ;
40- this . dialogCoordinator = dialogCoordinator ;
4139
4240 // Export commands
43- SaveAsSvgCommand = new SimpleCommand ( ( _ ) => SaveAsSvg_Execute ( ) , ( _ ) => SelectedIcon is IconViewModel ) ;
44- SaveAsWpfCommand = new SimpleCommand ( ( _ ) => SaveAsWpf_Execute ( ) , ( _ ) => SelectedIcon is not null ) ;
45- SaveAsUwpCommand = new SimpleCommand ( ( _ ) => SaveAsUwp_Execute ( ) , ( _ ) => SelectedIcon is not null ) ;
46-
47- SaveAsPngCommand = new SimpleCommand ( ( _ ) => SaveAsBitmapExecute ( new PngBitmapEncoder ( ) ) , ( _ ) => SelectedIcon is not null ) ;
48- SaveAsJpegCommand = new SimpleCommand ( ( _ ) => SaveAsBitmapExecute ( new JpegBitmapEncoder ( ) ) , ( _ ) => SelectedIcon is not null ) ;
49- SaveAsBmpCommand = new SimpleCommand ( ( _ ) => SaveAsBitmapExecute ( new BmpBitmapEncoder ( ) ) , ( _ ) => SelectedIcon is not null ) ;
41+ SaveAsSvgCommand = new AsyncCommand ( SaveAsSvgAsync , _ => SelectedIcon is IconViewModel ) ;
42+ SaveAsWpfCommand = new AsyncCommand ( SaveAsWpfAsync , _ => SelectedIcon is IconViewModel ) ;
43+ SaveAsUwpCommand = new AsyncCommand ( SaveAsUwpAsync , _ => SelectedIcon is IconViewModel ) ;
44+ SaveAsPngCommand = new AsyncCommand ( ( ) => SaveAsBitmapAsync ( new PngBitmapEncoder ( ) ) , _ => SelectedIcon is IconViewModel ) ;
45+ SaveAsJpegCommand = new AsyncCommand ( ( ) => SaveAsBitmapAsync ( new JpegBitmapEncoder ( ) ) , _ => SelectedIcon is IconViewModel ) ;
46+ SaveAsBmpCommand = new AsyncCommand ( ( ) => SaveAsBitmapAsync ( new BmpBitmapEncoder ( ) ) , _ => SelectedIcon is IconViewModel ) ;
5047 }
5148
52- public IconPackViewModel ( MainViewModel mainViewModel , Type enumType , Type packType , IDialogCoordinator dialogCoordinator )
53- : this ( mainViewModel , dialogCoordinator )
49+ public IconPackViewModel ( MainViewModel mainViewModel , Type enumType , Type packType )
50+ : this ( mainViewModel )
5451 {
5552 // Get the Name of the IconPack via Attributes
5653 this . MetaData = Attribute . GetCustomAttribute ( packType , typeof ( MetaDataAttribute ) ) as MetaDataAttribute ;
@@ -60,8 +57,8 @@ public IconPackViewModel(MainViewModel mainViewModel, Type enumType, Type packTy
6057 this . LoadEnumsAsync ( enumType , packType ) . SafeFireAndForget ( ) ;
6158 }
6259
63- public IconPackViewModel ( MainViewModel mainViewModel , string caption , Type [ ] enumTypes , Type [ ] packTypes , IDialogCoordinator dialogCoordinator )
64- : this ( mainViewModel , dialogCoordinator )
60+ public IconPackViewModel ( MainViewModel mainViewModel , string caption , Type [ ] enumTypes , Type [ ] packTypes )
61+ : this ( mainViewModel )
6562 {
6663 this . MainViewModel = mainViewModel ;
6764
@@ -183,13 +180,20 @@ public IIconViewModel SelectedIcon
183180 if ( Set ( ref _selectedIcon , value ) )
184181 {
185182 CommandManager . InvalidateRequerySuggested ( ) ;
183+
184+ SaveAsSvgCommand ? . RaiseCanExecuteChanged ( ) ;
185+ SaveAsWpfCommand ? . RaiseCanExecuteChanged ( ) ;
186+ SaveAsUwpCommand ? . RaiseCanExecuteChanged ( ) ;
187+ SaveAsPngCommand ? . RaiseCanExecuteChanged ( ) ;
188+ SaveAsJpegCommand ? . RaiseCanExecuteChanged ( ) ;
189+ SaveAsBmpCommand ? . RaiseCanExecuteChanged ( ) ;
186190 }
187191 }
188192 }
189193
190- public ICommand SaveAsSvgCommand { get ; }
194+ public IAsyncCommand SaveAsSvgCommand { get ; }
191195
192- private async void SaveAsSvg_Execute ( )
196+ private async Task SaveAsSvgAsync ( )
193197 {
194198 var progress = await dialogCoordinator . ShowProgressAsync ( MainViewModel , "Export" , "Saving selected icon as SVG-file" ) ;
195199 progress . SetIndeterminate ( ) ;
@@ -253,7 +257,11 @@ private async void SaveAsSvg_Execute()
253257
254258 var svgFileContent = ExportHelper . FillTemplate ( svgFileTemplate , parameters ) ;
255259
260+ #if NETFRAMEWORK
256261 using IO . StreamWriter file = new IO . StreamWriter ( fileSaveDialog . FileName ) ;
262+ #else
263+ await using IO . StreamWriter file = new IO . StreamWriter ( fileSaveDialog . FileName ) ;
264+ #endif
257265 await file . WriteAsync ( svgFileContent ) ;
258266 }
259267 }
@@ -265,9 +273,9 @@ private async void SaveAsSvg_Execute()
265273 await progress . CloseAsync ( ) ;
266274 }
267275
268- public ICommand SaveAsWpfCommand { get ; }
276+ public IAsyncCommand SaveAsWpfCommand { get ; }
269277
270- private async void SaveAsWpf_Execute ( )
278+ private async Task SaveAsWpfAsync ( )
271279 {
272280 var progress = await dialogCoordinator . ShowProgressAsync ( MainViewModel , "Export" , "Saving selected icon as WPF-XAML-file" ) ;
273281 progress . SetIndeterminate ( ) ;
@@ -317,7 +325,11 @@ private async void SaveAsWpf_Execute()
317325
318326 var wpfFileContent = ExportHelper . FillTemplate ( wpfFileTemplate , parameters ) ;
319327
328+ #if NETFRAMEWORK
320329 using IO . StreamWriter file = new IO . StreamWriter ( fileSaveDialog . FileName ) ;
330+ #else
331+ await using IO . StreamWriter file = new IO . StreamWriter ( fileSaveDialog . FileName ) ;
332+ #endif
321333 await file . WriteAsync ( wpfFileContent ) ;
322334 }
323335 }
@@ -329,9 +341,9 @@ private async void SaveAsWpf_Execute()
329341 await progress . CloseAsync ( ) ;
330342 }
331343
332- public ICommand SaveAsUwpCommand { get ; }
344+ public IAsyncCommand SaveAsUwpCommand { get ; }
333345
334- private async void SaveAsUwp_Execute ( )
346+ private async Task SaveAsUwpAsync ( )
335347 {
336348 var progress = await dialogCoordinator . ShowProgressAsync ( MainViewModel , "Export" , "Saving selected icon as WPF-XAML-file" ) ;
337349 progress . SetIndeterminate ( ) ;
@@ -380,7 +392,11 @@ private async void SaveAsUwp_Execute()
380392
381393 var wpfFileContent = ExportHelper . FillTemplate ( wpfFileTemplate , parameters ) ;
382394
395+ #if NETFRAMEWORK
383396 using IO . StreamWriter file = new IO . StreamWriter ( fileSaveDialog . FileName ) ;
397+ #else
398+ await using IO . StreamWriter file = new IO . StreamWriter ( fileSaveDialog . FileName ) ;
399+ #endif
384400 await file . WriteAsync ( wpfFileContent ) ;
385401 }
386402 }
@@ -392,13 +408,13 @@ private async void SaveAsUwp_Execute()
392408 await progress . CloseAsync ( ) ;
393409 }
394410
395- public ICommand SaveAsPngCommand { get ; }
411+ public IAsyncCommand SaveAsPngCommand { get ; }
396412
397- public ICommand SaveAsJpegCommand { get ; }
413+ public IAsyncCommand SaveAsJpegCommand { get ; }
398414
399- public ICommand SaveAsBmpCommand { get ; }
415+ public IAsyncCommand SaveAsBmpCommand { get ; }
400416
401- private async void SaveAsBitmapExecute ( BitmapEncoder encoder )
417+ private async Task SaveAsBitmapAsync ( BitmapEncoder encoder )
402418 {
403419 var progress = await dialogCoordinator . ShowProgressAsync ( MainViewModel , "Export" , "Saving selected icon as bitmap image" ) ;
404420 progress . SetIndeterminate ( ) ;
@@ -450,7 +466,11 @@ private async void SaveAsBitmapExecute(BitmapEncoder encoder)
450466
451467 encoder . Frames . Add ( BitmapFrame . Create ( renderTargetBitmap ) ) ;
452468
469+ #if NETFRAMEWORK
453470 using var fileStream = new IO . FileStream ( fileSaveDialog . FileName , IO . FileMode . Create ) ;
471+ #else
472+ await using var fileStream = new IO . FileStream ( fileSaveDialog . FileName , IO . FileMode . Create ) ;
473+ #endif
454474 encoder . Save ( fileStream ) ;
455475 }
456476 }
0 commit comments