wpfui icon indicating copy to clipboard operation
wpfui copied to clipboard

NavigationItem: can't figure out how to set initial page

Open chucker opened this issue 2 years ago • 2 comments

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:

  1. UpdateServiceItems gets called too soon, so the NavigationService iterates over an empty collection.
  2. It also doesn't initially have _frame set.

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

chucker avatar Aug 22 '23 09:08 chucker

Same problem for me. I hope this get fixed.

Porterbg avatar Sep 11 '23 13:09 Porterbg

@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.

frostybee avatar Jan 17 '24 05:01 frostybee