-
[DX_WPF] 시작하기 (MDI)DevExpress/DX_WPF 2024. 1. 10. 07:47
New Solution Project
파일 수정 (Module 동적로드)
App.xaml.cs
using System; using DevExpress.Mvvm; using DevExpress.Mvvm.ModuleInjection; using DevExpress.Mvvm.UI; using DevExpress.Xpf.Core; using DXModular.Common; using DXModular.Main.Properties; using DXModular.Main.ViewModels; using DXModular.Main.Views; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Reflection; using System.Windows; using AppModules = DXModular.Common.Modules; using Module = DevExpress.Mvvm.ModuleInjection.Module; namespace DXModular.Main { public partial class App : Application { public App() { ApplicationThemeHelper.UpdateApplicationThemeName(); SplashScreenManager.CreateThemed().ShowOnStartup(); } protected override void OnExit(ExitEventArgs e) { ApplicationThemeHelper.SaveApplicationThemeName(); base.OnExit(e); } void OnApplicationStartup(object sender, StartupEventArgs e) { Bootstrapper.Run(); } } public class Bootstrapper { private List<Assembly> assemblies = new List<Assembly>(); private List<string> keyNames = new List<string>(); public List<DevExpress.Mvvm.ModuleInjection.Module> modules = new List<DevExpress.Mvvm.ModuleInjection.Module>(); public static Bootstrapper Default { get; protected set; } public static void Run() { Default = new Bootstrapper(); Default.RunCore(); } protected Bootstrapper() { } const string StateVersion = "1.0"; public virtual void RunCore() { ConfigureTypeLocators(); RegisterModules(); if (!RestoreState()) InjectModules(); ConfigureNavigation(); ShowMainWindow(); } protected IModuleManager Manager { get { return ModuleManager.DefaultManager; } } protected virtual void ConfigureTypeLocators() { assemblies.Add(typeof(MainViewModel).Assembly); } protected virtual void RegisterModules() { Manager.GetRegion(Regions.Documents).VisualSerializationMode = VisualSerializationMode.PerKey; Manager.Register(Regions.MainWindow, new Module(AppModules.Main, MainViewModel.Create, typeof(MainView))); // First, find all modules var foundModules = Directory.EnumerateFiles(System.Environment.CurrentDirectory, "DXModular.*.dll", SearchOption.AllDirectories); if (foundModules.Count() == 0) return; foreach (var item in foundModules) { var asm = Assembly.LoadFrom(item); foreach (Type type in asm.GetTypes()) { MethodInfo info = type.GetMethod("CreateModules"); if (info == null) continue; assemblies.Add(asm); string keyField = (string)type.GetProperty("Key").GetValue(type); // returns ModuleViewModel string viewName = (string)type.GetProperty("ViewName").GetValue(type); // returns ModuleView keyNames.Add(keyField); info.Invoke(null, new object[] { }); //var module = new DevExpress.Mvvm.ModuleInjection.Module(keyField, () => info.Invoke(null, new object[] { $"{keyField}Module" }), viewName); //modules.Add(module); } } // Update view locator(s) ViewModelLocator.Default = new ViewModelLocator(assemblies); ViewLocator.Default = new ViewLocator(assemblies); } protected virtual bool RestoreState() { #if !DEBUG if (Settings.Default.StateVersion != StateVersion) return false; return Manager.Restore(Settings.Default.LogicalState, Settings.Default.VisualState); #else return false; #endif } protected virtual void InjectModules() { Manager.Inject(Regions.MainWindow, AppModules.Main); } protected virtual void ConfigureNavigation() { Manager.GetEvents(Regions.Navigation).Navigation += OnNavigation; Manager.GetEvents(Regions.Documents).Navigation += OnDocumentsNavigation; } protected virtual void ShowMainWindow() { App.Current.MainWindow = new MainWindow(); App.Current.MainWindow.Show(); App.Current.MainWindow.Closing += OnClosing; } void OnNavigation(object sender, NavigationEventArgs e) { if (e.NewViewModelKey == null) return; Manager.InjectOrNavigate(Regions.Documents, e.NewViewModelKey); } void OnDocumentsNavigation(object sender, NavigationEventArgs e) { Manager.Navigate(Regions.Navigation, e.NewViewModelKey); } void OnClosing(object sender, CancelEventArgs e) { string logicalState; string visualState; Manager.Save(out logicalState, out visualState); Settings.Default.StateVersion = StateVersion; Settings.Default.LogicalState = logicalState; Settings.Default.VisualState = visualState; Settings.Default.Save(); } } }
New Project Module
<ImplicitUsings>enable</ImplicitUsings> 삭제
DevExpress.Wpf Nuget 추가
View 추가
ViewModel 추가
Views
<UserControl x:Class="DXModular.ModuleA.Views.ModuleAView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:views="clr-namespace:DXModular.ModuleA.Views" xmlns:viewModels="clr-namespace:DXModular.ModuleA.ViewModels" xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{dxmvvm:ViewModelSource viewModels:ModuleAViewModel}"> <Grid> </Grid> </UserControl>
ViewModels
internal => public
https://github.com/dlsenfl3/DXApplication1
ViewModel with Constructor Injection in Xaml
728x90'DevExpress > DX_WPF' 카테고리의 다른 글
[DX_WPF] ISupportServices (0) 2024.01.19 [DX_WPF] DevExpress 다국어 지원 (0) 2024.01.12 [DX_WPF] DockLayoutManager SaveLayout (0) 2023.12.14 [DX_WPF] DevExpress Create a Custom Service (0) 2023.11.29 [DX_WPF] xpf ~ Reference (1) 2023.11.28 댓글