Home > OS >  WinUI 3 - How to have navigate to NavigateViewItem when the app starts?
WinUI 3 - How to have navigate to NavigateViewItem when the app starts?

Time:11-19

I'm developing a WinUI 3 application and one thing I can't find or understand is how can I have a (NavigationViewItem, basically Home tab, homePage) to be opened when the app is launched. I have App.xaml and MainWindow.xaml. I've found another question on Stackoverflow, mainly here however this does use ViewModel but I'm not using ViewModel. How can I achieve this behavior?

Here's the MainWindow.xaml

<Window
    x:Class="Ankara_Online.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Ankara_Online"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <StackPanel>
        <NavigationView x:Name="_navigationView"
            IsSettingsVisible="False"
            IsBackButtonVisible="Collapsed"
            IsBackEnabled="False"
            Header="Home Page"
            AlwaysShowHeader="False"
            PaneTitle="navigationPane"
            PaneDisplayMode="Top"
            ExpandedModeThresholdWidth="500"
            SelectionFollowsFocus="Disabled"
            IsTabStop="False">
            <NavigationView.Resources>
                <SolidColorBrush x:Key="NavigationViewTopPaneBackground" Color="#195caa" />
            </NavigationView.Resources>
            <NavigationView.MenuItems>
                <NavigationViewItem Content="Home" Tag="homePage" x:Name="homePageItem">
                    <NavigationViewItem.Icon>
                        <SymbolIcon Symbol="Play" />
                    </NavigationViewItem.Icon>
                </NavigationViewItem>
                <NavigationViewItem Content="Applications and Sector Files" Tag="softwareSectorFilePage" x:Name="softwareSectorFilePageItem">
                    <NavigationViewItem.Icon>
                        <SymbolIcon Symbol="Save" />
                    </NavigationViewItem.Icon>
                </NavigationViewItem>
                <NavigationViewItem Content="Documentation" Tag="DocumentationPage" x:Name="DocumentationPageItem">
                    <NavigationViewItem.Icon>
                        <SymbolIcon Symbol="Refresh" />
                    </NavigationViewItem.Icon>
                </NavigationViewItem>
                <NavigationViewItem Content="Settings" Tag="SettingsPage" x:Name="SettingsPageItem">
                    <NavigationViewItem.Icon>
                        <SymbolIcon Symbol="Refresh" />
                    </NavigationViewItem.Icon>
                </NavigationViewItem>
            </NavigationView.MenuItems>

            <NavigationView.PaneCustomContent>
                <HyperlinkButton x:Name="PaneHyperlink" Content="More info" Margin="12,0" Visibility="Collapsed" />
            </NavigationView.PaneCustomContent>

            <NavigationView.PaneFooter>
                <StackPanel x:Name="FooterStackPanel" Orientation="Vertical" Visibility="Visible">
                    <NavigationViewItem Icon="Download" AutomationProperties.Name="download" />
                </StackPanel>
            </NavigationView.PaneFooter>
            <Frame x:Name="contentFrame" />
        </NavigationView>
    </StackPanel>
</Window>

CodePudding user response:

Assuming that you have a HomePage.xaml, you can do it this way:

MainWindow.xaml

<Window
    x:Class="NavigationViewTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <NavigationView x:Name="NavigationViewControl">
            <NavigationView.MenuItems>
                <NavigationViewItem
                    Content="Home"
                    Tag="NavigationViewTest.HomePage" />
                <NavigationViewItem
                    Content="Page A"
                    Tag="NavigationViewTest.PageA" />
            </NavigationView.MenuItems>
            <Frame x:Name="ContentFrame" />
        </NavigationView>
    </Grid>

</Window>

MainWindow.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;
using System.Linq;

namespace NavigationViewTest;

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        NavigateByTag("NavigationViewTest.HomePage");
        this.NavigationViewControl.SelectionChanged  = NavigationViewControl_SelectionChanged;
    }

    private void NavigationViewControl_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
    {
        if (args.SelectedItem is NavigationViewItem item &&
            item.Tag is string tag)
        {
            NavigateByTag(tag);
        }
    }

    private void NavigateByTag(string tag)
    {
        if (this.NavigationViewControl.MenuItems
            .OfType<NavigationViewItem>()
            .Where(x => x.Tag.Equals(tag) is true)
            .FirstOrDefault() is NavigationViewItem item)
        {
            this.NavigationViewControl.SelectedItem = item;
            this.ContentFrame.Navigate(Type.GetType($"{item.Tag}"));
        }
    }
}
  • Related