Skip to content

Commit

Permalink
Merge pull request #1712 from jp-weber/NavigationView
Browse files Browse the repository at this point in the history
Update NavigationViewEx
  • Loading branch information
JerryNixon authored Nov 16, 2019
2 parents f5b2b98 + d6c41e1 commit deecff6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 109 deletions.
149 changes: 42 additions & 107 deletions Source/Template10.Controls.NavViewEx/NavViewEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using System.Collections.ObjectModel;
using win = Windows;
using System.Threading;
using Template10.Navigation;
using Template10.Services;
using NavigationView = Microsoft.UI.Xaml.Controls.NavigationView;
using NavigationViewItem = Microsoft.UI.Xaml.Controls.NavigationViewItem;

namespace Template10.Controls
{
public class NavViewEx : NavigationView
{
private Button _togglePaneButton;
private TextBlock _paneTitleTextBlock;
private Button _backButton;
private readonly CoreDispatcher _dispatcher;
private readonly Frame _frame;

Expand All @@ -38,7 +36,7 @@ public NavViewEx()
{
if (TryFindItem(e.SourcePageType, e.Parameter, out var item))
{
SetSelectedItem(item);
SetSelectedItem(item, false);
}
};

Expand All @@ -53,7 +51,6 @@ public NavViewEx()

RegisterPropertyChangedCallback(IsPaneOpenProperty, (s, e) =>
{
UpdateAppTitleVisibility();
UpdatePaneHeadersVisibility();
});

Expand All @@ -64,94 +61,13 @@ public NavViewEx()

Loaded += (s, e) =>
{
UpdateAppTitleVisibility();
UpdatePaneHeadersVisibility();
UpdatePageHeaderContent();
SetupBackButton();
};
}

public INavigationService NavigationService { get; }

private void SetupBackButton()
{
var children = XamlUtilities.RecurseChildren(this);
var grids = children.OfType<Grid>();
var grid = grids.Single(x => x.Name == "TogglePaneTopPadding");
grid.Visibility = Visibility.Collapsed;

grid = grids.Single(x => x.Name == "ContentPaneTopPadding");
grid.RegisterPropertyChangedCallback(HeightProperty, (s, args) =>
{
if (grid.Height != 44d)
{
grid.Height = 44d;
}
});
grid.Height = 44d;

var child_buttons = children.OfType<Button>();

_togglePaneButton = child_buttons.Single(x => x.Name == "TogglePaneButton");
_togglePaneButton.RegisterPropertyChangedCallback(MarginProperty, (s, args) =>
{
if (_togglePaneButton.Margin.Top != 0)
{
_togglePaneButton.Margin = new Thickness(0, 0, 0, 32);
}
});
_togglePaneButton.Margin = new Thickness(0, 0, 0, 32);
_togglePaneButton.Focus(FocusState.Programmatic);

var parent_grid = _togglePaneButton.Parent as Grid;
parent_grid.Width = double.NaN;
parent_grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(48) });
parent_grid.ColumnDefinitions.Add(new ColumnDefinition { });
parent_grid.RowDefinitions[0].Height = GridLength.Auto;
parent_grid.RowDefinitions[1].Height = GridLength.Auto;

_paneTitleTextBlock = new TextBlock
{
Name = "PaneTitleTextBlock",
Margin = new Thickness(8, 18, 0, 0),
FontSize = 15,
FontFamily = new FontFamily("Segoe UI Bold"),
TextWrapping = TextWrapping.NoWrap,
Foreground = Resources["SystemControlForegroundBaseHighBrush"] as Brush,
VerticalAlignment = VerticalAlignment.Top,
IsHitTestVisible = false,
Text = "Jerry Nixon",
};
_paneTitleTextBlock.SetValue(Grid.ColumnProperty, 1);
_paneTitleTextBlock.SetValue(Grid.RowProperty, 1);
_paneTitleTextBlock.SetValue(Canvas.ZIndexProperty, 100);
// parent_grid.Children.Add(_paneTitleTextBlock);

_backButton = new Button
{
Name = "BackButton",
Content = new SymbolIcon
{
Symbol = Symbol.Back,
IsHitTestVisible = false
},
Style = Resources["PaneToggleButtonStyle"] as Style,
};
_backButton.SetValue(Canvas.ZIndexProperty, 100);
parent_grid.Children.Insert(1, _backButton);

NavigationService.CanGoBackChanged += (s, args) =>
{
_backButton.IsEnabled = NavigationService.CanGoBack();
};

_backButton.Click += (s, args) =>
{
var gesture_service = GestureService.GetForCurrentView();
gesture_service.RaiseBackRequested();
};
}

public INavigationService Initialize()
{
var first = MenuItems
Expand All @@ -172,41 +88,71 @@ public enum ItemHeaderBehaviors { Hide, Remove, None }
public Uri SettingsNavigationUri { get; set; }
public event EventHandler SettingsInvoked;

private object PreviousItem
{
get; set;
}

public new object SelectedItem
{
set => SetSelectedItem(value);
get => base.SelectedItem;
}

private async void SetSelectedItem(object selectedItem)
private async void SetSelectedItem(object selectedItem, bool withNavigation = true)
{
if (selectedItem == null)
{
base.SelectedItem = null;
}
else if (selectedItem == base.SelectedItem)
else if (selectedItem == PreviousItem)
{
// already set
}
else if (selectedItem == SettingsItem)
{
if (SettingsNavigationUri != null)
{
await NavigationService.NavigateAsync(SettingsNavigationUri);
base.SelectedItem = selectedItem;
if (withNavigation)
{
if ((await NavigationService.NavigateAsync(SettingsNavigationUri)).Success)
{
PreviousItem = selectedItem;
base.SelectedItem = selectedItem;
SettingsInvoked?.Invoke(this, EventArgs.Empty);
}
else
{
base.SelectedItem = null;
Debug.WriteLine($"{selectedItem}.{nameof(NavViewProps.NavigationUriProperty)} navigation failed.");
}
}
else
{
PreviousItem = selectedItem;
base.SelectedItem = selectedItem;
}


}
SettingsInvoked?.Invoke(this, EventArgs.Empty);
}
else if (selectedItem is NavigationViewItem item)
{
if (item.GetValue(NavViewProps.NavigationUriProperty) is string path)
{
if ((await NavigationService.NavigateAsync(path)).Success)
if (!withNavigation)
{
PreviousItem = item;
base.SelectedItem = item;
}
else if ((await NavigationService.NavigateAsync(path)).Success)
{
PreviousItem = selectedItem;
base.SelectedItem = selectedItem;
}
else
{
base.SelectedItem = PreviousItem;
Debug.WriteLine($"{selectedItem}.{nameof(NavViewProps.NavigationUriProperty)} navigation failed.");
}
}
Expand All @@ -215,17 +161,6 @@ private async void SetSelectedItem(object selectedItem)
Debug.WriteLine($"{selectedItem}.{nameof(NavViewProps.NavigationUriProperty)} is not valid Uri");
}
}
UpdatePaneHeadersVisibility();
UpdatePageHeaderContent();
}

private void UpdateAppTitleVisibility()
{
if (_paneTitleTextBlock != null)
{
_paneTitleTextBlock.Visibility = IsPaneOpen
? Visibility.Visible : Visibility.Collapsed;
}
}

private void UpdatePaneHeadersVisibility()
Expand Down Expand Up @@ -315,15 +250,15 @@ private bool TryFindItem(Type type, object parameter, out object item)
{
// registered?

if (!Navigation.PageRegistry.TryGetRegistration(type, out var info))
if (!PageRegistry.TryGetRegistration(type, out var info))
{
item = null;
return false;
}

// search settings

if (Navigation.NavigationQueue.TryParse(SettingsNavigationUri, null, out var settings))
if (NavigationQueue.TryParse(SettingsNavigationUri, null, out var settings))
{
if (type == settings.Last().View && (string)parameter == settings.Last().QueryString)
{
Expand Down Expand Up @@ -351,7 +286,7 @@ private bool TryFindItem(Type type, object parameter, out object item)

foreach (var menuItem in menuItems)
{
if (Navigation.NavigationQueue.TryParse(menuItem.Path, null, out var menuQueue)
if (NavigationQueue.TryParse(menuItem.Path, null, out var menuQueue)
&& Equals(menuQueue.Last().View, type))
{
item = menuItem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="MSBuild.Sdk.Extras">

<PropertyGroup>
<TargetFrameworks>uap10.0.16299</TargetFrameworks>
<TargetFrameworks>uap10.0.18362</TargetFrameworks>
<RootNamespace>Template10</RootNamespace>
<Title>Template10 Controls NavViewEx</Title>
<Description>Template10 Controls NavViewEx</Description>
Expand All @@ -22,7 +22,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Template10.Core\Template10.Core.csproj" />
<PackageReference Include="Microsoft.UI.Xaml" Version="2.2.190917002" />
<PackageReference Include="Template10" Version="2.0.1" />
</ItemGroup>

</Project>

0 comments on commit deecff6

Please sign in to comment.