Skip to content

Commit

Permalink
Restore navigation-related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabacr07 committed Jul 13, 2023
1 parent 6fe6a17 commit 6b9862d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion src/MetroTrilithon.Desktop/MetroTrilithon.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<TargetFrameworks>net7.0-windows;net6.0-windows;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
Expand Down
124 changes: 61 additions & 63 deletions src/MetroTrilithon.Desktop/UI/Controls/NavigationHelper.cs
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
}
2 changes: 1 addition & 1 deletion src/MetroTrilithon/MetroTrilithon.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Product>MetroTrilithon</Product>
Expand Down

0 comments on commit 6b9862d

Please sign in to comment.