/* 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 System; using CommonServiceLocator; using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Ioc; using GalaSoft.MvvmLight.Views; using MediatR; using Microsoft.Extensions.DependencyInjection; using ModernKeePass.Application.Common.Interfaces; using ModernKeePass.ViewModels.ListItems; namespace ModernKeePass.ViewModels { /// /// 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();IDataService SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); SimpleIoc.Default.Register(() => App.Services.GetRequiredService()); } SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); SimpleIoc.Default.Register(); } public MainVm Main => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SettingsVm Settings => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SettingsDatabaseVm SettingsDatabase => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SettingsNewVm SettingsNew => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SettingsSaveVm SettingsSave => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SettingsSecurityVm SettingsSecurity => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public OpenDatabaseControlVm OpenDatabaseControl => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SetCredentialsVm SetCredentials => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public NewVm New => ServiceLocator.Current.GetInstance(); public OpenVm Open => ServiceLocator.Current.GetInstance(); public RecentVm Recent => ServiceLocator.Current.GetInstance(Guid.NewGuid().ToString()); public SaveVm Save => ServiceLocator.Current.GetInstance(); public static void Cleanup() { // TODO Clear the ViewModels SimpleIoc.Default.Reset(); } } }