Dreamine.MVVM.Wpf 1.0.3
Dreamine.MVVM.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DreamineAppBuilder.cs
이 파일의 문서화 페이지로 가기
1using Dreamine.MVVM.Core;
2using Dreamine.MVVM.Core.Locators;
3using Dreamine.MVVM.Interfaces.Navigation;
4using Dreamine.MVVM.Interfaces.Windows;
5using Dreamine.MVVM.Locators;
6using Dreamine.MVVM.Locators.Wpf;
7using System;
8using System.Reflection;
9using System.Windows;
10using System.Windows.Controls;
11
13{
22 public static class DreamineAppBuilder
23 {
32 private static readonly object SyncRoot = new();
51
68 public static void Initialize(Assembly rootAssembly)
69 {
71 }
72
97 public static void Initialize(Assembly rootAssembly, DreamineWpfOptions options)
98 {
99 ArgumentNullException.ThrowIfNull(rootAssembly);
100 ArgumentNullException.ThrowIfNull(options);
101
102 if (options.RegisterDefaultServices)
103 {
105 }
106
108
109 // ① DI 자동 등록
110 DreamineAutoRegistrar.RegisterAll(rootAssembly);
111
112 // ② View ↔ ViewModel 자동 매핑
113 ViewModelLocator.RegisterAll(rootAssembly);
114
115 // ③ 선택적 Loaded 기반 ViewModel 자동 주입
117 {
119 }
120
121 // ④ 선택적 Region 기반 INavigator 자동 등록
123 {
125 }
126 }
127
137 {
138 ViewModelLocator.RegisterResolver(
139 new DreamineContainerViewModelResolver());
140 }
141
166 public static void RegisterDefaultServices(DreamineWpfOptions? options = null)
167 {
168 if (!DMContainer.IsRegistered<IWindowStateService>())
169 {
170 DMContainer.RegisterSingleton<IWindowStateService, WindowStateService>();
171 }
172
173 if (!DMContainer.IsRegistered<IViewManager>())
174 {
175 var vm = options is not null
176 ? new ViewManager(DMContainer.GetResolver(),
177 options.FallbackWindowWidth,
178 options.FallbackWindowHeight)
179 : new ViewManager();
180 DMContainer.RegisterSingleton<IViewManager>(vm);
181 }
182
183 if (!DMContainer.IsRegistered<IDialogService>())
184 {
185 DMContainer.RegisterSingleton<IDialogService, WpfDialogService>();
186 }
187 }
188
198 {
199 lock (SyncRoot)
200 {
202 {
203 return;
204 }
205
206 EventManager.RegisterClassHandler(
207 typeof(FrameworkElement),
208 FrameworkElement.LoadedEvent,
209 new RoutedEventHandler(AttachViewModelIfExists));
210
212 }
213 }
214
239 public static void RegisterAutoNavigatorHandlerOnce(string defaultRegionName)
240 {
241 if (string.IsNullOrWhiteSpace(defaultRegionName))
242 {
243 throw new ArgumentException("Default region name must not be empty.", nameof(defaultRegionName));
244 }
245
246 lock (SyncRoot)
247 {
249 {
250 return;
251 }
252
253 EventManager.RegisterClassHandler(
254 typeof(Window),
255 FrameworkElement.LoadedEvent,
256 new RoutedEventHandler((sender, _) =>
257 {
258 if (sender is Window window)
259 {
260 RegisterNavigatorFromWindow(window, defaultRegionName);
261 }
262 }));
263
265 }
266 }
267
300 public static void RegisterNavigatorFromWindow(Window window, string regionName)
301 {
302 ArgumentNullException.ThrowIfNull(window);
303
304 if (string.IsNullOrWhiteSpace(regionName))
305 {
306 throw new ArgumentException("Region name must not be empty.", nameof(regionName));
307 }
308
309 ContentControl? region = RegionBinderHelper.FindRegionControl(window, regionName);
310 if (region is null)
311 {
312 return;
313 }
314
315 DMContainer.RegisterSingleton<INavigator>(
316 new ContentControlNavigator(region));
317 }
318
343 private static void AttachViewModelIfExists(object sender, RoutedEventArgs e)
344 {
345 if (sender is not FrameworkElement view)
346 {
347 return;
348 }
349
350 if (view.DataContext is not null)
351 {
352 return;
353 }
354
355 if (view is not Window && view is not UserControl && view is not Page)
356 {
357 return;
358 }
359
360 object? viewModel = ViewModelLocator.Resolve(view.GetType());
361 if (viewModel is not null)
362 {
363 view.DataContext = viewModel;
364 }
365 }
366 }
367}
static void Initialize(Assembly rootAssembly)
static void RegisterAutoNavigatorHandlerOnce(string defaultRegionName)
static void AttachViewModelIfExists(object sender, RoutedEventArgs e)
static void RegisterNavigatorFromWindow(Window window, string regionName)
static void Initialize(Assembly rootAssembly, DreamineWpfOptions options)
static void RegisterDefaultServices(DreamineWpfOptions? options=null)
static DreamineWpfOptions CreateDefault()