Dreamine.UI.Wpf.Controls 1.0.1
Dreamine.UI.Wpf.Controls 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
ViewSwitcher.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Collections.Generic;
3using System.Windows;
4using System.Windows.Controls;
5using System.Windows.Forms;
6using System.Xml.Linq;
7using Dreamine.MVVM.Interfaces;
8
10{
19 public class ViewSwitcher
20 {
29 private readonly IReadOnlyDictionary<string, Window> _popupWindows;
38 private readonly Dictionary<string, FrameworkElement> _views;
47 private readonly bool _useSingleton;
48
90 IReadOnlyDictionary<string, Window> popupWindows,
91 Dictionary<string, FrameworkElement> views,
92 bool useSingletonView)
93 {
94 _popupWindows = popupWindows ?? throw new ArgumentNullException(nameof(popupWindows));
95 _views = views ?? throw new ArgumentNullException(nameof(views));
96 _useSingleton = useSingletonView;
97 }
98
123 public bool IsPopupName(string viewName)
124 {
125 if (string.IsNullOrWhiteSpace(viewName)) return false;
126 return _popupWindows.ContainsKey(viewName);
127 }
128
137 private static string _popupName = null!;
138
147 public static string CurrentPopupName => _popupName;
148
189 public void Switch(string viewName, bool popupChange)
190 {
191 if (!_useSingleton) return;
192
193 // Popup target
194 if (IsPopupName(viewName))
195 {
196 var popup = _popupWindows[viewName];
197
198 //var vm = DMContainer.Resolve(vmType);
199 if (!popup.IsVisible)
200 {
201 NotifyShown(viewName);
202 _popupName = viewName;
203 popup.Show();
204 }
205 popup.Activate();
206 return;
207 }
208
209 // View target
210 if (!popupChange)
211 {
212 // CloseAsync popups only when caller indicates a non-popup-change flow.
213 foreach (var dlg in _popupWindows.Values)
214 {
215 if (dlg.IsVisible)
216 {
218 dlg.Hide();
219 }
220 }
221 }
222
223 foreach (var kv in _views)
224 {
225 kv.Value.Visibility = kv.Key == viewName
226 ? Visibility.Visible
227 : Visibility.Hidden;
228 }
229 }
230
239 private static readonly Dictionary<string, object> _viewModelRegistry = new();
240
273 public static void RegisterViewModel(string viewName, object vm)
274 => _viewModelRegistry[viewName] = vm;
275
300 public static void NotifyShown(string viewName)
301 {
302 if (!_viewModelRegistry.TryGetValue(viewName, out var vm)) return;
303 if (vm is IActivatable act) act.Activate();
304 if (vm is IVisibilityAware vis) vis.OnShown();
305 }
306
331 public static void NotifyHidden(string viewName)
332 {
333 if (!_viewModelRegistry.TryGetValue(viewName, out var vm)) return;
334 if (vm is IVisibilityAware vis) vis.OnHidden();
335 if (vm is IActivatable act) act.Deactivate();
336 }
337 }
338}
readonly IReadOnlyDictionary< string, Window > _popupWindows
ViewSwitcher(IReadOnlyDictionary< string, Window > popupWindows, Dictionary< string, FrameworkElement > views, bool useSingletonView)
readonly Dictionary< string, FrameworkElement > _views
void Switch(string viewName, bool popupChange)
static readonly Dictionary< string, object > _viewModelRegistry
static void RegisterViewModel(string viewName, object vm)