Skip to content

Commit cbefac4

Browse files
authored
Merge pull request #4131 from XAML-Knight/ListDetailsView_RemoveHardcoded
Update string reliance on WinUI NavigationView
2 parents 8ecc496 + 2978876 commit cbefac4

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/ListDetailsView/ListDetailsView.bind

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,20 @@
7474
</controls:ListDetailsView.NoSelectionContentTemplate>
7575
<controls:ListDetailsView.ListCommandBar>
7676
<CommandBar>
77+
<!-- Button functionality to be implemented by developer -->
7778
<AppBarButton Icon="Back" Label="Back"/>
7879
<AppBarButton Icon="Forward" Label="Forward"/>
79-
80-
<CommandBar.Content>
81-
<TextBlock Margin="12,14">
82-
<Run Text="{Binding Emails.Count}" />
83-
<Run Text="Items" />
84-
</TextBlock>
85-
</CommandBar.Content>
86-
</CommandBar>
80+
<CommandBar.Content>
81+
<TextBlock Margin="12,14">
82+
<Run Text="{Binding Emails.Count}" />
83+
<Run Text="Items" />
84+
</TextBlock>
85+
</CommandBar.Content>
86+
</CommandBar>
8787
</controls:ListDetailsView.ListCommandBar>
8888
<controls:ListDetailsView.DetailsCommandBar>
8989
<CommandBar>
90+
<!-- Button functionality to be implemented by developer -->
9091
<AppBarButton Icon="MailReply" Label="Reply" />
9192
<AppBarButton Icon="MailReplyAll" Label="Reply All" />
9293
<AppBarButton Icon="MailForward" Label="Forward" />

Microsoft.Toolkit.Uwp.UI.Controls.Layout/ListDetailsView/ListDetailsView.cs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Windows.UI.Xaml.Controls;
1111
using Windows.UI.Xaml.Input;
1212
using Windows.UI.Xaml.Navigation;
13+
using NavigationView = Microsoft.UI.Xaml.Controls;
1314

1415
namespace Microsoft.Toolkit.Uwp.UI.Controls
1516
{
@@ -42,12 +43,11 @@ public partial class ListDetailsView : ItemsControl
4243
private AppViewBackButtonVisibility? _previousSystemBackButtonVisibility;
4344
private bool _previousNavigationViewBackEnabled;
4445

45-
// Int used because the underlying type is an enum, but we don't have access to the enum
46-
private int _previousNavigationViewBackVisibilty;
46+
private NavigationView.NavigationViewBackButtonVisible _previousNavigationViewBackVisibilty;
47+
private NavigationView.NavigationView _navigationView;
4748
private ContentPresenter _detailsPresenter;
4849
private VisualStateGroup _selectionStateGroup;
4950
private Button _inlineBackButton;
50-
private object _navigationView;
5151
private Frame _frame;
5252

5353
/// <summary>
@@ -199,7 +199,7 @@ private void OnLoaded(object sender, RoutedEventArgs e)
199199
_frame.Navigating -= OnFrameNavigating;
200200
}
201201

202-
_navigationView = this.FindAscendants().FirstOrDefault(p => p.GetType().FullName == "Microsoft.UI.Xaml.Controls.NavigationView");
202+
_navigationView = this.FindAscendant<NavigationView.NavigationView>();
203203
_frame = this.FindAscendant<Frame>();
204204
if (_frame != null)
205205
{
@@ -328,8 +328,6 @@ private void UpdateView(bool animate)
328328
/// </summary>
329329
private void SetBackButtonVisibility(ListDetailsViewState? previousState = null)
330330
{
331-
const int backButtonVisible = 1;
332-
333331
if (DesignMode.DesignModeEnabled)
334332
{
335333
return;
@@ -358,7 +356,7 @@ private void SetBackButtonVisibility(ListDetailsViewState? previousState = null)
358356
}
359357
else
360358
{
361-
SetNavigationViewBackButtonState(backButtonVisible, true);
359+
SetNavigationViewBackButtonState(NavigationView.NavigationViewBackButtonVisible.Visible, true);
362360
}
363361
}
364362
else if (BackButtonBehavior != BackButtonBehavior.Manual)
@@ -441,27 +439,18 @@ private void SetVisualState(bool animate)
441439
VisualStateManager.GoToState(this, SelectedItem == null ? noSelectionState : hasSelectionState, animate);
442440
}
443441

444-
private void SetNavigationViewBackButtonState(int visible, bool enabled)
442+
private void SetNavigationViewBackButtonState(NavigationView.NavigationViewBackButtonVisible visibility, bool enabled)
445443
{
446444
if (_navigationView == null)
447445
{
448446
return;
449447
}
450448

451-
var navType = _navigationView.GetType();
452-
var visibleProperty = navType.GetProperty("IsBackButtonVisible");
453-
if (visibleProperty != null)
454-
{
455-
_previousNavigationViewBackVisibilty = (int)visibleProperty.GetValue(_navigationView);
456-
visibleProperty.SetValue(_navigationView, visible);
457-
}
449+
_previousNavigationViewBackVisibilty = _navigationView.IsBackButtonVisible;
450+
_navigationView.IsBackButtonVisible = visibility;
458451

459-
var enabledProperty = navType.GetProperty("IsBackEnabled");
460-
if (enabledProperty != null)
461-
{
462-
_previousNavigationViewBackEnabled = (bool)enabledProperty.GetValue(_navigationView);
463-
enabledProperty.SetValue(_navigationView, enabled);
464-
}
452+
_previousNavigationViewBackEnabled = _navigationView.IsBackEnabled;
453+
_navigationView.IsBackEnabled = enabled;
465454
}
466455

467456
private void SetDetailsContent()

0 commit comments

Comments
 (0)