From a1085b6010a751b9f3130f6f1aa02e963eab48ce Mon Sep 17 00:00:00 2001 From: Geoffroy BONNEVILLE Date: Tue, 21 Apr 2020 13:07:17 +0200 Subject: [PATCH] Add MVVM Light library Updated nuget packages --- ModernKeePass/App.xaml | 37 +++++++------ ModernKeePass/App.xaml.cs | 14 +++-- ModernKeePass/DependencyInjection.cs | 14 ++++- ModernKeePass/ViewModel/MainViewModel.cs | 34 ++++++++++++ ModernKeePass/ViewModel/ViewModelLocator.cs | 55 +++++++++++++++++++ ModernKeePass/ViewModels/EntryDetailVm.cs | 2 +- ModernKeePass/ViewModels/GroupDetailVm.cs | 2 +- ModernKeePass/ViewModels/RecentVm.cs | 2 +- ModernKeePass/ViewModels/SaveVm.cs | 2 +- .../UpdateCredentialsUserControl.xaml | 26 ++++----- .../UpdateCredentialsUserControl.xaml.cs | 2 +- ModernKeePass/Win81App.csproj | 40 ++++++++++---- ModernKeePass/packages.config | 25 +++++---- WinAppCommon/Common/Constants.cs | 7 +++ ...iewModel.cs => SetCredentialsViewModel.cs} | 6 +- WinAppCommon/WinAppCommon.projitems | 2 +- 16 files changed, 204 insertions(+), 66 deletions(-) create mode 100644 ModernKeePass/ViewModel/MainViewModel.cs create mode 100644 ModernKeePass/ViewModel/ViewModelLocator.cs rename WinAppCommon/ViewModels/UserControls/{UpdateCredentialsViewModel.cs => SetCredentialsViewModel.cs} (81%) diff --git a/ModernKeePass/App.xaml b/ModernKeePass/App.xaml index 1e96ea1..175a021 100644 --- a/ModernKeePass/App.xaml +++ b/ModernKeePass/App.xaml @@ -1,18 +1,21 @@ - - - - - - - - - - - + + + + + - - + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ModernKeePass/App.xaml.cs b/ModernKeePass/App.xaml.cs index 240f2a2..9cf0302 100644 --- a/ModernKeePass/App.xaml.cs +++ b/ModernKeePass/App.xaml.cs @@ -10,6 +10,7 @@ using Windows.Storage.Pickers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; +using GalaSoft.MvvmLight.Views; using MediatR; using Microsoft.Extensions.DependencyInjection; using Microsoft.HockeyApp; @@ -37,6 +38,7 @@ namespace ModernKeePass private readonly IResourceProxy _resource; private readonly IMediator _mediator; private readonly ISettingsProxy _settings; + private readonly INavigationService _navigation; public static IServiceProvider Services { get; private set; } @@ -62,12 +64,13 @@ namespace ModernKeePass serviceCollection.AddInfrastructureCommon(); serviceCollection.AddInfrastructureKeePass(); serviceCollection.AddInfrastructureUwp(); - serviceCollection.AddAppAutomapper(); + serviceCollection.AddWin81App(); Services = serviceCollection.BuildServiceProvider(); _mediator = Services.GetService(); _resource = Services.GetService(); _settings = Services.GetService(); + _navigation = Services.GetService(); } #region Event Handlers @@ -157,7 +160,8 @@ namespace ModernKeePass var launchActivatedEventArgs = e as LaunchActivatedEventArgs; if (launchActivatedEventArgs != null && rootFrame.Content == null) - rootFrame.Navigate(typeof(MainPage), launchActivatedEventArgs.Arguments); + //rootFrame.Navigate(typeof(MainPage), launchActivatedEventArgs.Arguments); + _navigation.NavigateTo(Constants.Navigation.MainPage, launchActivatedEventArgs.Arguments); // Ensure the current window is active Window.Current.Activate(); @@ -247,11 +251,13 @@ namespace ModernKeePass Name = file.DisplayName, Path = file.Path }; - rootFrame.Navigate(typeof(MainPage), fileInfo); + //rootFrame.Navigate(typeof(MainPage), fileInfo); + _navigation.NavigateTo(Constants.Navigation.MainPage, fileInfo); } else { - rootFrame.Navigate(typeof(MainPage)); + //rootFrame.Navigate(typeof(MainPage)); + _navigation.NavigateTo(Constants.Navigation.MainPage); } Window.Current.Content = rootFrame; diff --git a/ModernKeePass/DependencyInjection.cs b/ModernKeePass/DependencyInjection.cs index 9c18c87..2be2630 100644 --- a/ModernKeePass/DependencyInjection.cs +++ b/ModernKeePass/DependencyInjection.cs @@ -1,17 +1,29 @@ using System.Reflection; using AutoMapper; +using GalaSoft.MvvmLight.Views; using Microsoft.Extensions.DependencyInjection; +using ModernKeePass.Common; +using ModernKeePass.Views; namespace ModernKeePass { public static class DependencyInjection { - public static IServiceCollection AddAppAutomapper(this IServiceCollection services) + public static IServiceCollection AddWin81App(this IServiceCollection services) { var applicationAssembly = typeof(Application.DependencyInjection).GetTypeInfo().Assembly; var infrastructureAssembly = typeof(Infrastructure.DependencyInjection).GetTypeInfo().Assembly; services.AddAutoMapper(applicationAssembly, infrastructureAssembly); + services.AddSingleton(provider => + { + var nav = new NavigationService(); + nav.Configure(Constants.Navigation.MainPage, typeof(MainPage)); + nav.Configure(Constants.Navigation.EntryPage, typeof(EntryDetailPage)); + nav.Configure(Constants.Navigation.GroupPage, typeof(GroupDetailPage)); + return nav; + }); + return services; } } diff --git a/ModernKeePass/ViewModel/MainViewModel.cs b/ModernKeePass/ViewModel/MainViewModel.cs new file mode 100644 index 0000000..5d6d57d --- /dev/null +++ b/ModernKeePass/ViewModel/MainViewModel.cs @@ -0,0 +1,34 @@ +using GalaSoft.MvvmLight; + +namespace ModernKeePass.ViewModel +{ + /// + /// This class contains properties that the main View can data bind to. + /// + /// Use the mvvminpc snippet to add bindable properties to this ViewModel. + /// + /// + /// You can also use Blend to data bind with the tool's support. + /// + /// + /// See http://www.galasoft.ch/mvvm + /// + /// + public class MainViewModel : ViewModelBase + { + /// + /// Initializes a new instance of the MainViewModel class. + /// + public MainViewModel() + { + ////if (IsInDesignMode) + ////{ + //// // Code runs in Blend --> create design time data. + ////} + ////else + ////{ + //// // Code runs "for real" + ////} + } + } +} \ No newline at end of file diff --git a/ModernKeePass/ViewModel/ViewModelLocator.cs b/ModernKeePass/ViewModel/ViewModelLocator.cs new file mode 100644 index 0000000..dbbd121 --- /dev/null +++ b/ModernKeePass/ViewModel/ViewModelLocator.cs @@ -0,0 +1,55 @@ +/* + In App.xaml: + + + + + In the View: + DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}" + + You can also use Blend to do all this with the tool's support. + See http://www.galasoft.ch/mvvm +*/ + +using CommonServiceLocator; +using GalaSoft.MvvmLight; +using GalaSoft.MvvmLight.Ioc; + +namespace ModernKeePass.ViewModel +{ + /// + /// This class contains static references to all the view models in the + /// application and provides an entry point for the bindings. + /// + public class ViewModelLocator + { + /// + /// Initializes a new instance of the ViewModelLocator class. + /// + public ViewModelLocator() + { + ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); + + ////if (ViewModelBase.IsInDesignModeStatic) + ////{ + //// // Create design time view services and models + //// SimpleIoc.Default.Register(); + ////} + ////else + ////{ + //// // Create run time view services and models + //// SimpleIoc.Default.Register(); + ////} + + SimpleIoc.Default.Register(); + } + + public MainViewModel Main => ServiceLocator.Current.GetInstance(); + + public static void Cleanup() + { + // TODO Clear the ViewModels + } + } +} \ No newline at end of file diff --git a/ModernKeePass/ViewModels/EntryDetailVm.cs b/ModernKeePass/ViewModels/EntryDetailVm.cs index d15e68d..ad53837 100644 --- a/ModernKeePass/ViewModels/EntryDetailVm.cs +++ b/ModernKeePass/ViewModels/EntryDetailVm.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Threading.Tasks; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media; +using GalaSoft.MvvmLight.Command; using MediatR; using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Database.Commands.SaveDatabase; @@ -22,7 +23,6 @@ using ModernKeePass.Application.Group.Commands.RemoveEntry; using ModernKeePass.Application.Group.Queries.GetGroup; using ModernKeePass.Application.Security.Commands.GeneratePassword; using ModernKeePass.Application.Security.Queries.EstimatePasswordComplexity; -using ModernKeePass.Common; using ModernKeePass.Domain.Enums; using ModernKeePass.Interfaces; using ModernKeePass.Application.Group.Models; diff --git a/ModernKeePass/ViewModels/GroupDetailVm.cs b/ModernKeePass/ViewModels/GroupDetailVm.cs index 825d583..0684829 100644 --- a/ModernKeePass/ViewModels/GroupDetailVm.cs +++ b/ModernKeePass/ViewModels/GroupDetailVm.cs @@ -5,6 +5,7 @@ using System.Collections.Specialized; using System.Linq; using System.Threading.Tasks; using Windows.UI.Xaml.Controls; +using GalaSoft.MvvmLight.Command; using MediatR; using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Database.Commands.SaveDatabase; @@ -24,7 +25,6 @@ using ModernKeePass.Application.Group.Commands.UpdateGroup; using ModernKeePass.Application.Group.Models; using ModernKeePass.Application.Group.Queries.GetGroup; using ModernKeePass.Application.Group.Queries.SearchEntries; -using ModernKeePass.Common; using ModernKeePass.Domain.AOP; using ModernKeePass.Domain.Enums; using ModernKeePass.Interfaces; diff --git a/ModernKeePass/ViewModels/RecentVm.cs b/ModernKeePass/ViewModels/RecentVm.cs index ba05d04..1b5e287 100644 --- a/ModernKeePass/ViewModels/RecentVm.cs +++ b/ModernKeePass/ViewModels/RecentVm.cs @@ -1,9 +1,9 @@ using System.Collections.ObjectModel; using System.Linq; using System.Windows.Input; +using GalaSoft.MvvmLight.Command; using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Common.Interfaces; -using ModernKeePass.Common; using ModernKeePass.Domain.AOP; using ModernKeePass.Domain.Interfaces; using ModernKeePass.ViewModels.ListItems; diff --git a/ModernKeePass/ViewModels/SaveVm.cs b/ModernKeePass/ViewModels/SaveVm.cs index e2b2695..15f48e2 100644 --- a/ModernKeePass/ViewModels/SaveVm.cs +++ b/ModernKeePass/ViewModels/SaveVm.cs @@ -2,12 +2,12 @@ using Windows.Storage; using Windows.Storage.AccessCache; using Windows.UI.Xaml.Controls; +using GalaSoft.MvvmLight.Command; using MediatR; using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Database.Commands.CloseDatabase; using ModernKeePass.Application.Database.Commands.SaveDatabase; using ModernKeePass.Application.Database.Queries.GetDatabase; -using ModernKeePass.Common; using ModernKeePass.Views; namespace ModernKeePass.ViewModels diff --git a/ModernKeePass/Views/UserControls/UpdateCredentialsUserControl.xaml b/ModernKeePass/Views/UserControls/UpdateCredentialsUserControl.xaml index 62dde4b..7f4e119 100644 --- a/ModernKeePass/Views/UserControls/UpdateCredentialsUserControl.xaml +++ b/ModernKeePass/Views/UserControls/UpdateCredentialsUserControl.xaml @@ -1,14 +1,14 @@ - + @@ -18,7 +18,7 @@ - + @@ -60,7 +60,7 @@