NavigationItem: can't figure out how to set initial page
Describe the bug
I have a bunch of navigation items:
<wpfui:NavigationFluent
x:Name="Navigation"
Frame="{Binding ElementName=ContentFrame}"
SelectedPageIndex="0">
<wpfui:NavigationFluent.Items>
<wpfui:NavigationItem
Content="Title 1"
Icon="Folder20"
IconSize="20"
PageType="{x:Type local:View1}" />
<wpfui:NavigationItem
Content="Title 2"
Icon="Folder20"
IconSize="20"
PageType="{x:Type local:View2}" />
<wpfui:NavigationItem
Content="Title 2"
Icon="Folder20"
IconSize="20"
PageType="{x:Type local:View3}" />
</wpfui:NavigationFluent.Items>
</wpfui:NavigationFluent>
The navigation items work; if I click something in the sidebar, a page gets instantiated and shown, even with animation. However, I have SelectedPageIndex="0", so the first page should automatically show, and it does not. The pages do correctly get tags (PageTag is set), so that doesn't appear to be the issue.
I surmise from the code that there are two problems here:
-
UpdateServiceItemsgets called too soon, so theNavigationServiceiterates over an empty collection. - It also doesn't initially have
_frameset.
Both lead to it ultimately returning false.
I was able to hack around it with reflection as follows:
var navigationBaseType = typeof(NavigationBase);
_ = navigationBaseType.GetMethod("UpdateServiceItems", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).Invoke(Navigation, null);
var navigationService = navigationBaseType.GetField("_navigationService", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(Navigation);
navigationService.GetType().GetField("_frame", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).SetValue(navigationService, ContentFrame);
_ = Navigation.Navigate("view1");
Now, View1 gets animated into place as soon as the window opens.
To Reproduce
Expected behavior
NavigationFluent should be sufficiently initialized such that either SelectedPageIndex or calling Navigate() in Loaded does something meaningful.
Screenshots
No response
OS version
Windows 11 23521
.NET version
.NET Framework 4.7.2
WPF-UI NuGet version
2.1.0
Additional context
No response
Same problem for me. I hope this get fixed.
@chucker @Porterbg Have you explored the code of the Wpf.Ui.Demo.Simple app? If you do, you will notice the following line in MainWindow.xaml.cs: Loaded += (_, _) => RootNavigation.Navigate(typeof(DashboardPage)); // where DashboardPage is set as initial page.
The above line sets the initial page to which the application should navigate to once the main window is loaded. Hope this helps.