-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restore navigation-related functions
- Loading branch information
Showing
3 changed files
with
63 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 61 additions & 63 deletions
124
src/MetroTrilithon.Desktop/UI/Controls/NavigationHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,61 @@ | ||
//using System; | ||
//using System.Collections.Generic; | ||
//using System.Linq; | ||
//using System.Reactive.Linq; | ||
//using System.Windows; | ||
//using System.Windows.Navigation; | ||
//using Microsoft.Xaml.Behaviors; | ||
//using Wpf.Ui.Common; | ||
//using Wpf.Ui.Controls; | ||
//using Wpf.Ui.Controls.Navigation; | ||
|
||
//namespace MetroTrilithon.UI.Controls; | ||
|
||
//public class NavigationHelper | ||
//{ | ||
// #region PropagationContext attached property | ||
|
||
// private static readonly HashSet<NavigationBase> _knownItems = new(); | ||
|
||
// public static readonly DependencyProperty PropagationContextProperty | ||
// = DependencyProperty.RegisterAttached( | ||
// nameof(PropagationContextProperty).GetPropertyName(), | ||
// typeof(object), | ||
// typeof(NavigationHelper), | ||
// new PropertyMetadata(null, HandlePropagationContextChanged)); | ||
|
||
// public static void SetPropagationContext(DependencyObject element, object value) | ||
// => element.SetValue(PropagationContextProperty, value); | ||
|
||
// public static object GetPropagationContext(DependencyObject element) | ||
// => element.GetValue(PropagationContextProperty); | ||
|
||
// private static void HandlePropagationContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
// { | ||
// if (d is not NavigationItem item) throw new NotSupportedException($"The '{nameof(PropagationContextProperty).GetPropertyName()}' attached property is only supported for the '{nameof(NavigationItem)}' type."); | ||
|
||
// var navigation = item.GetSelfAndAncestors() | ||
// .OfType<NavigationBase>() | ||
// .FirstOrDefault(); | ||
// if (navigation?.Frame == null || _knownItems.Add(navigation) == false) return; | ||
|
||
// Observable.FromEvent<RoutedNavigationEvent, RoutedNavigationEventArgs>( | ||
//#pragma warning disable CS8622 | ||
// handler => (_, args) => handler(args), | ||
//#pragma warning restore CS8622 | ||
// handler => navigation.Navigated += handler, | ||
// handler => navigation.Navigated -= handler) | ||
// .Zip(Observable.FromEvent<NavigatedEventHandler, NavigationEventArgs>( | ||
// handler => (_, args) => handler(args), | ||
// handler => navigation.Frame.Navigated += handler, | ||
// handler => navigation.Frame.Navigated -= handler)) | ||
// .Subscribe(args => | ||
// { | ||
// if (args.First.CurrentPage is NavigationItem navigationItem | ||
// && args.Second.Content is FrameworkElement element) | ||
// { | ||
// element.DataContext = GetPropagationContext(navigationItem); | ||
// } | ||
// }); | ||
// } | ||
|
||
// #endregion | ||
//} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using System.Windows; | ||
using Microsoft.Xaml.Behaviors; | ||
using Wpf.Ui.Common; | ||
using Wpf.Ui.Controls; | ||
|
||
namespace MetroTrilithon.UI.Controls; | ||
|
||
public class NavigationHelper | ||
{ | ||
#region PropagationContext attached property | ||
|
||
private static readonly HashSet<NavigationView> _knownItems = new(); | ||
|
||
public static readonly DependencyProperty PropagationContextProperty | ||
= DependencyProperty.RegisterAttached( | ||
nameof(PropagationContextProperty).GetPropertyName(), | ||
typeof(object), | ||
typeof(NavigationHelper), | ||
new PropertyMetadata(null, HandlePropagationContextChanged)); | ||
|
||
public static void SetPropagationContext(DependencyObject element, object value) | ||
=> element.SetValue(PropagationContextProperty, value); | ||
|
||
public static object GetPropagationContext(DependencyObject element) | ||
=> element.GetValue(PropagationContextProperty); | ||
|
||
private static void HandlePropagationContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is not NavigationViewItem item) | ||
{ | ||
throw new NotSupportedException($"The '{nameof(PropagationContextProperty).GetPropertyName()}' attached property is only supported for the '{nameof(NavigationViewItem)}' type."); | ||
} | ||
|
||
var navigation = item.GetSelfAndAncestors() | ||
.OfType<NavigationView>() | ||
.FirstOrDefault(); | ||
if (navigation == null || _knownItems.Add(navigation) == false) return; | ||
|
||
Observable.FromEvent<TypedEventHandler<NavigationView, NavigatedEventArgs>, NavigatedEventArgs>( | ||
handler => (_, args) => handler(args), | ||
handler => navigation.Navigated += handler, | ||
handler => navigation.Navigated -= handler) | ||
.Zip(Observable.FromEvent<TypedEventHandler<NavigationView, RoutedEventArgs>, RoutedEventArgs>( | ||
handler => (_, args) => handler(args), | ||
handler => navigation.SelectionChanged += handler, | ||
handler => navigation.SelectionChanged -= handler)) | ||
.Subscribe(args => | ||
{ | ||
if (args is { First.Page: FrameworkElement element, Second.Source: NavigationView { SelectedItem: NavigationViewItem selected} }) | ||
{ | ||
element.DataContext = GetPropagationContext(selected); | ||
} | ||
}); | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters