|
| 1 | +using System; |
| 2 | + |
| 3 | +using Windows.UI.Xaml; |
| 4 | +using Windows.UI.Xaml.Controls; |
| 5 | +using Windows.UI.Xaml.Media.Animation; |
| 6 | +using Windows.UI.Xaml.Navigation; |
| 7 | + |
| 8 | +namespace OneDrive_Cloud_Player.Services |
| 9 | +{ |
| 10 | + public static class NavigationService |
| 11 | + { |
| 12 | + public static event NavigatedEventHandler Navigated; |
| 13 | + |
| 14 | + public static event NavigationFailedEventHandler NavigationFailed; |
| 15 | + |
| 16 | + private static Frame _frame; |
| 17 | + private static object _lastParamUsed; |
| 18 | + |
| 19 | + public static Frame Frame |
| 20 | + { |
| 21 | + get |
| 22 | + { |
| 23 | + if (_frame == null) |
| 24 | + { |
| 25 | + _frame = Window.Current.Content as Frame; |
| 26 | + RegisterFrameEvents(); |
| 27 | + } |
| 28 | + |
| 29 | + return _frame; |
| 30 | + } |
| 31 | + |
| 32 | + set |
| 33 | + { |
| 34 | + UnregisterFrameEvents(); |
| 35 | + _frame = value; |
| 36 | + RegisterFrameEvents(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static bool CanGoBack => Frame.CanGoBack; |
| 41 | + |
| 42 | + public static bool CanGoForward => Frame.CanGoForward; |
| 43 | + |
| 44 | + public static bool GoBack() |
| 45 | + { |
| 46 | + if (CanGoBack) |
| 47 | + { |
| 48 | + Frame.GoBack(); |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + public static void GoForward() => Frame.GoForward(); |
| 56 | + |
| 57 | + public static bool Navigate(Type pageType, object parameter = null, NavigationTransitionInfo infoOverride = null) |
| 58 | + { |
| 59 | + if (pageType == null || !pageType.IsSubclassOf(typeof(Page))) |
| 60 | + { |
| 61 | + throw new ArgumentException($"Invalid pageType '{pageType}', please provide a valid pageType.", nameof(pageType)); |
| 62 | + } |
| 63 | + |
| 64 | + // Don't open the same page multiple times |
| 65 | + if (Frame.Content?.GetType() != pageType || (parameter != null && !parameter.Equals(_lastParamUsed))) |
| 66 | + { |
| 67 | + var navigationResult = Frame.Navigate(pageType, parameter, infoOverride); |
| 68 | + if (navigationResult) |
| 69 | + { |
| 70 | + _lastParamUsed = parameter; |
| 71 | + } |
| 72 | + |
| 73 | + return navigationResult; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + return false; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static bool Navigate<T>(object parameter = null, NavigationTransitionInfo infoOverride = null) |
| 82 | + where T : Page |
| 83 | + { |
| 84 | + return Navigate(typeof(T), parameter, infoOverride); |
| 85 | + } |
| 86 | + |
| 87 | + private static void RegisterFrameEvents() |
| 88 | + { |
| 89 | + if (_frame != null) |
| 90 | + { |
| 91 | + _frame.Navigated += Frame_Navigated; |
| 92 | + _frame.NavigationFailed += Frame_NavigationFailed; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static void UnregisterFrameEvents() |
| 97 | + { |
| 98 | + if (_frame != null) |
| 99 | + { |
| 100 | + _frame.Navigated -= Frame_Navigated; |
| 101 | + _frame.NavigationFailed -= Frame_NavigationFailed; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static void Frame_NavigationFailed(object sender, NavigationFailedEventArgs e) => NavigationFailed?.Invoke(sender, e); |
| 106 | + |
| 107 | + private static void Frame_Navigated(object sender, NavigationEventArgs e) => Navigated?.Invoke(sender, e); |
| 108 | + } |
| 109 | +} |
0 commit comments