Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
NavigationService.cs
1using System.Reflection.Metadata;
4using System.Windows.Navigation;
5
9
11
16{
17 private readonly IPageService _pageService;
18 private Frame _frame;
19 private object _lastParameterUsed;
20
22 public event EventHandler<string> Navigated;
23
25 public bool CanGoBack => _frame.CanGoBack;
26
28 public FrameworkElement CurrentFrameContent => _frame.Content as FrameworkElement;
29
31 public object CurrentFrameViewModel => CurrentFrameContent?.DataContext;
32
37 public NavigationService(IPageService pageService)
38 {
39 _pageService = pageService;
40 }
41
43 public void Initialize(Frame shellFrame)
44 {
45 if (_frame == null)
46 {
47 _frame = shellFrame;
48 _frame.Navigated += OnNavigated;
49 }
50 }
51
54 {
55 _frame.Navigated -= OnNavigated;
56 _frame = null;
57 }
58
60 public void GoBack()
61 {
62 if (_frame.CanGoBack)
63 {
64 var vmBeforeNavigation = _frame.GetDataContext();
65 _frame.GoBack();
66 if (vmBeforeNavigation is INavigationAware navigationAware)
67 {
68 navigationAware.OnNavigatedFrom();
69 }
70 }
71 }
72
74 public bool NavigateTo(string pageKey, object parameter = null, bool clearNavigation = false)
75 {
76 var pageType = _pageService.GetPageType(pageKey);
77
78 if (_frame.Content?.GetType() != pageType || (parameter != null && !parameter.Equals(_lastParameterUsed)))
79 {
80 _frame.Tag = clearNavigation;
81 var page = _pageService.GetPage(pageKey);
82 var navigated = _frame.Navigate(page, parameter);
83 if (navigated)
84 {
85 _lastParameterUsed = parameter;
86 var dataContext = _frame.GetDataContext();
87 if (dataContext is INavigationAware navigationAware)
88 {
89 navigationAware.OnNavigatedFrom();
90 }
91 }
92
93 return navigated;
94 }
95
96 return false;
97 }
98
100 public bool NavigateTo<T_VM>(object parameter = null, bool clearNavigation = false)
101 {
102 return NavigateTo(typeof(T_VM).FullName);
103 }
104
106 public bool ReloadCurrent()
107 {
108 string pageKey = CurrentFrameViewModel.GetType().FullName;
109 var pageType = _pageService.GetPageType(pageKey);
110
111 _frame.Tag = false; // clearNavigation
112 var page = _pageService.GetPage(pageKey);
113 var navigated = _frame.Navigate(page, _lastParameterUsed);
114 if (navigated)
115 {
116 var dataContext = _frame.GetDataContext();
117 if (dataContext is INavigationAware navigationAware)
118 {
119 navigationAware.OnNavigatedFrom();
120 }
121 }
122 return navigated;
123 }
124
126 public void CleanNavigation()
127 => _frame.CleanNavigation();
128
129 private void OnNavigated(object sender, NavigationEventArgs e)
130 {
131 if (sender is Frame frame)
132 {
133 bool clearNavigation = (bool)frame.Tag;
134 if (clearNavigation)
135 {
136 frame.CleanNavigation();
137 }
138
139 var dataContext = frame.GetDataContext();
140 if (dataContext is INavigationAware navigationAware)
141 {
142 navigationAware.OnNavigatedTo(e.ExtraData);
143 }
144
145 Navigated?.Invoke(sender, dataContext.GetType().FullName);
146 }
147 }
148}
void Initialize(Frame shellFrame)
Initializes the navigation service with the provided frame.
void CleanNavigation()
Cleans the navigation stack of the frame.
bool ReloadCurrent()
Navigate to the current page again.This can be used to trigger the INavigationAware....
void GoBack()
Goes back to the previous page in the navigation stack if possible.
bool CanGoBack
True if the frame can navigate back, false otherwise.
FrameworkElement CurrentFrameContent
Current FrameworkElement that is displayed in the Frame.Most times this is a page.
NavigationService(IPageService pageService)
Constructor for the NavigationService.
void UnsubscribeNavigation()
Unsubscribes from navigation events and clears the frame reference.
bool NavigateTo< T_VM >(object parameter=null, bool clearNavigation=false)
Navigates to the specified page using its view model type.True on navigation success
object CurrentFrameViewModel
View model used by the CurrentFrameContent
bool NavigateTo(string pageKey, object parameter=null, bool clearNavigation=false)
Navigates to the specified page using its key.True on navigation success
Interface for a service for handling navigation within the application.
Interface for a service to manage pages in the application.
Interface for objects that need to handle navigation events.