XAML WinUI 3 -如何在应用程序启动时导航到NavigateViewItem?

6ovsh4lw  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(189)

我正在开发一个WinUI 3应用程序,有一件事我无法找到或理解,那就是如何在启动应用程序时打开(NavigationViewItem,基本上是Home选项卡,homePage)。我有App.xaml和MainWindow.xaml。我在Stackoverflow上发现了另一个问题,主要是here,但它确实使用了ViewModel,但我没有使用ViewModel。我如何实现这种行为?
这是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>
ig9co6j1

ig9co6j11#

假设你有一个HomePage.xaml,你可以这样做:

主窗口.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>

主窗口.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}"));
        }
    }
}

相关问题