//Sort type for listview of drivers public MainPageViewModel() { //Initialize collection of drivers var controller = new DriverBackup(); Drivers = new ObservableCollection <DriverInformation>(controller.ListDrivers(AppSettings.ShowMicrosoftDrivers)); //Init Driver box VM //Init top Buttons var top = new ObservableCollection <ActionButton>() { new ActionButton(StringResources.DriverID, ActionButton.ButtonType.NoHighlight, "\xEA37", "DriverId"), new ActionButton(StringResources.Description, ActionButton.ButtonType.NoHighlight, "\xE7C3", "Description"), new ActionButton(StringResources.Backup, ActionButton.ButtonType.NoHighlight, "\xE896", "Backup"), }; //Init bot buttons var bot = new ObservableCollection <ActionButton>() { new ActionButton(StringResources.Save, SaveSelectedDrivers, ActionButton.ButtonType.Accept, "\xE74E"), new ActionButton(StringResources.SelectAll, SelectAll, ActionButton.ButtonType.Deafult, "\xE133"), }; DriversBox = new DriversBoxViewModel(Drivers, top, bot); }
private async Task SaveDriversAsync(IEnumerable <DriverInformation> drivers, string path, CancellationToken ct) { await Task.Run(async() => { try { var controller = new DriverBackup(); foreach (var t in drivers) { //Backup drivers one by one on background thread and show progress to the user await controller.BackupDriverAsync(t, path); await Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(() => Progress++)); ct.ThrowIfCancellationRequested(); } //Cancellation token for zipping cts = new CancellationTokenSource(); //Zip folder if user wants it automatically if (AppSettings.ZipRootFolder) { await CompressFolderAsZip(path); } //Alert user when the job is done MessageDialog = new MessageDialogViewModel( new ObservableCollection <ActionButton>(new List <ActionButton> { new ActionButton(StringResources.OK, () => MessageDialog = null, ActionButton.ButtonType.Accept), new ActionButton(StringResources.OpenFolder, () => OpenOutputFolder(path), ActionButton.ButtonType.Deafult), }), StringResources.DriversSaved, StringResources.DriversSavedLong); //Add compress folder as zip button if it is not automatic if (!AppSettings.ZipRootFolder) { MessageDialog.ActionButtons.Add( new ActionButton(StringResources.ZipFolder, async() => { MessageDialog = null; await CompressFolderAsZip(path); }, ActionButton.ButtonType.Deafult)); } } catch (OperationCanceledException) { //Canceled by user MessageDialog = new MessageDialogViewModel( new ObservableCollection <ActionButton>(new List <ActionButton> { new ActionButton(StringResources.OK, () => MessageDialog = null, ActionButton.ButtonType.Accept) }), StringResources.SavingCanceled); } catch (Exception e) { //Let user know about the error MessageDialog = new MessageDialogViewModel( new ObservableCollection <ActionButton>(new List <ActionButton> { new ActionButton(StringResources.OK, () => MessageDialog = null, ActionButton.ButtonType.Accept) }), StringResources.Error, e.Message); } finally { ShowInProgressDialog = false; } }, ct); }