using System; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Input; using Katteker; using Prism.Commands; using Prism.Mvvm; namespace ExampleCore { public class MainWindowViewModel : BindableBase { private readonly UpdateManager _updateManager; public static string WindowTitle { get; } = "Example Core " + Assembly.GetExecutingAssembly().GetName().Version?.ToString(3); public MainWindowViewModel() { UpdateManager.TryCreate(out _updateManager); UpdateCommand = new DelegateCommand(async x => await UpdateAsync(x.GetValueOrDefault()).ConfigureAwait(false)); OpenFolderDialogCommand = new DelegateCommand(OpenFolder); OpenFileDialogCommand = new DelegateCommand(OpenFile); } private static void OpenFile() { using var openFile = new OpenFileDialog(); if (openFile.ShowDialog() == DialogResult.OK) { MessageBox.Show(openFile.FileName, WindowTitle); } } private static void OpenFolder() { using var folderBrowser = new FolderBrowserDialog(); if (folderBrowser.ShowDialog() == DialogResult.OK) { MessageBox.Show(folderBrowser.SelectedPath, WindowTitle); } } private async Task UpdateAsync(bool startup) { try { var updates = await _updateManager.CheckForUpdatesAsync().ConfigureAwait(false); if (updates.Any()) { var dialogResult = MessageBox.Show("Update available, update now?", WindowTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dialogResult == DialogResult.Yes && await _updateManager.UpdateAppAsync().ConfigureAwait(false)) { dialogResult = MessageBox.Show("The update is ready to install. Do you want to install it now?", WindowTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) System.Windows.Application.Current.Shutdown(); } } else if (!startup) { MessageBox.Show("No Update available.", WindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception e) { if (startup) { MessageBox.Show(e.Message, WindowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } public ICommand UpdateCommand { get; } public ICommand OpenFolderDialogCommand { get; } public ICommand OpenFileDialogCommand { get; } public string CommandLineParameters => Environment.CommandLine; } }