135 bool isPopup = typeName.EndsWith(
"_Popup", StringComparison.OrdinalIgnoreCase);
136 string actualTypeName = isPopup ? typeName[..^6] : typeName;
140 Type.GetType(actualTypeName, throwOnError:
false, ignoreCase:
true)
141 ?? AppDomain.CurrentDomain.GetAssemblies()
142 .SelectManySafe(a => a.GetTypesSafe())
144 t.FullName?.Equals(actualTypeName, StringComparison.OrdinalIgnoreCase) ==
true ||
145 t.Name.Equals(actualTypeName.Split(
'.').Last(), StringComparison.OrdinalIgnoreCase));
151 bool resetDataContext = viewModelType !=
null;
153 object? vmInstance =
null;
154 string? uniqueKey =
null;
157 if (viewModelType !=
null)
159 if (useSingletonView)
161 vmInstance = DMContainer.Resolve(viewModelType) ?? Activator.CreateInstance(viewModelType);
167 uniqueKey = $
"{typeName}_{typeIndex:D2}";
169 vmInstance = Activator.CreateInstance(viewModelType);
177 if (vmInstance !=
null)
184 if (framework is UserControl uc)
193 if (wrapper.DataContext ==
null)
195 wrapper.DataContext = framework.DataContext;
202 FrameworkView = framework,
203 ViewModelType = viewModelType,
205 UniqueKey = uniqueKey
292 var candidates =
new List<string>();
295 candidates.Add(actualTypeName +
"ViewModel");
299 if (actualTypeName.Contains(
".Pages.", StringComparison.OrdinalIgnoreCase))
301 var pagesToVm = actualTypeName.Replace(
".Pages.",
".ViewModels.", StringComparison.OrdinalIgnoreCase);
302 candidates.Add(pagesToVm +
"ViewModel");
307 if (actualTypeName.Contains(
".Views.", StringComparison.OrdinalIgnoreCase))
309 var viewsToVm = actualTypeName.Replace(
".Views.",
".ViewModels.", StringComparison.OrdinalIgnoreCase);
310 candidates.Add(viewsToVm +
"ViewModel");
315 if (viewType !=
null)
317 string viewNs = viewType.Namespace ??
string.Empty;
318 string viewName = viewType.Name;
320 if (!
string.IsNullOrWhiteSpace(viewNs))
322 candidates.Add($
"{viewNs}.ViewModels.{viewName}ViewModel");
323 candidates.Add($
"{viewNs}.ViewModels.{viewNameNoSuffix}ViewModel");
324 candidates.Add($
"{viewNs}.{viewName}ViewModel");
325 candidates.Add($
"{viewNs}.{viewNameNoSuffix}ViewModel");
328 if (viewNs.Contains(
".Views", StringComparison.OrdinalIgnoreCase))
330 string swappedNs = viewNs.Replace(
".Views",
".ViewModels", StringComparison.OrdinalIgnoreCase);
331 candidates.Add($
"{swappedNs}.{viewName}ViewModel");
332 candidates.Add($
"{swappedNs}.{viewNameNoSuffix}ViewModel");
337 candidates.Add(viewName +
"ViewModel");
338 candidates.Add(viewNameNoSuffix +
"ViewModel");
342 candidates = candidates
343 .Where(x => !
string.IsNullOrWhiteSpace(x))
344 .Distinct(StringComparer.OrdinalIgnoreCase)
348 foreach (var cand
in candidates)
350 var t = AppDomain.CurrentDomain.GetAssemblies()
351 .SelectManySafe(a => a.GetTypesSafe())
353 x.FullName?.Equals(cand, StringComparison.OrdinalIgnoreCase) ==
true ||
354 x.Name.Equals(cand.Split(
'.').Last(), StringComparison.OrdinalIgnoreCase));
362 if (!t.IsClass || t.IsAbstract)
368 if (typeof(FrameworkElement).IsAssignableFrom(t))
374 if (!typeof(ViewModelBase).IsAssignableFrom(t))
435 if (viewType ==
null || viewType.IsAbstract)
439 Text = $
"[No View Type: {typeName}]",
440 Margin =
new Thickness(8)
444 object? instance =
null;
449 instance = DMContainer.Resolve(viewType);
453 System.Diagnostics.Debug.WriteLine($
"[ViewLoader] VsContainer.Resolve 실패: {typeName} - {ex.Message}");
457 if (instance ==
null)
461 instance = Activator.CreateInstance(viewType);
467 Text = $
"[CreateInstance Failed: {typeName}] {ex.Message}",
468 Margin =
new Thickness(8)
478 if (resetDataContext)
480 uc.DataContext =
null;
488 if (resetDataContext)
490 page.DataContext =
null;
493 var frame =
new Frame
496 NavigationUIVisibility = NavigationUIVisibility.Hidden,
497 JournalOwnership = JournalOwnership.OwnsJournal
500 if (resetDataContext)
502 frame.DataContext =
null;
511 if (win.Content is FrameworkElement content)
513 if (resetDataContext)
515 content.DataContext =
null;
516 win.DataContext =
null;
520 var host =
new ContentControl
525 if (resetDataContext)
527 host.DataContext =
null;
536 Text = $
"[Window has no Content: {typeName}]",
537 Margin =
new Thickness(8)
542 case FrameworkElement fe:
544 if (resetDataContext)
546 fe.DataContext =
null;
555 Text = $
"[Unknown View Type: {viewType.FullName}]",
556 Margin =
new Thickness(8)
596 if (root ==
null || vm ==
null)
602 if (root is Frame fr)
608#pragma warning disable CS1587
627 void TrySet(FrameworkElement fe)
634#pragma warning restore CS1587
637#pragma warning disable CS1587
664 void OnLoaded(object? s, RoutedEventArgs e)
666 if (s is FrameworkElement fe)
668 fe.Loaded -= OnLoaded;
672#pragma warning restore CS1587
675#pragma warning disable CS1587
702 void OnNavigated(object? s, NavigationEventArgs e)
704 fr.Navigated -= OnNavigated;
706 if (fr.Content is FrameworkElement fe)
710 fe.Loaded += OnLoaded;
716#pragma warning restore CS1587
719 if (fr.Content is FrameworkElement current)
721 if (!current.IsLoaded)
723 current.Loaded += OnLoaded;
731 fr.Navigated += OnNavigated;
738 if (root is ContentControl cc && cc.Content is FrameworkElement innerCc)
741 innerCc.DataContext = vm;
746 root.DataContext = vm;
790 private static void MergeResources(ResourceDictionary target, ResourceDictionary source)
792 foreach (var rd
in source.MergedDictionaries)
793 target.MergedDictionaries.Add(rd);
795 foreach (var key
in source.Keys)
796 if (!target.Contains(key))
797 target.Add(key, source[key]);