如何在 UWP 中通过 GoBack() 跟踪所选项目

How to keep track of selected item by GoBack() in UWP

我写了一个代码,我可以在其中转到不同的页面,然后可以使用 GoBack() 方法返回。

我的问题是,当我转到某个页面时,导航菜单中所选项目的名称旁边会出现一个蓝色条。但是当我返回时,所选项目不会改变。我该怎么做?

希望我能解释清楚。如果我不是,请考虑查看我添加的图像。

谢谢。

这是我的代码。

XAML

<NavigationView x:Name="navSample"
                IsPaneOpen="False"
                SelectionChanged="NavSample_SelectionChanged"
                SelectionFollowsFocus="Enabled" 
                IsBackButtonVisible="Auto"
                BackRequested="NavSample_BackRequested">
    <NavigationView.MenuItems>
        <NavigationViewItem Icon="Play" Content="Item1" x:Name="SamplePage1Item" />
        <NavigationViewItem Icon="Save" Content="Item2" x:Name="SamplePage2Item" />
        <NavigationViewItem Icon="Refresh" Content="Item3" x:Name="SamplePage3Item" />
    </NavigationView.MenuItems>
    <Frame x:Name="contentFrame"/>
</NavigationView>

C#

private void NavSample_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
{
    if (navSample.SelectedItem == SamplePage1Item)
    {
        contentFrame.Navigate(typeof(SamplePage1));
    }
    else if (navSample.SelectedItem == SamplePage2Item)
    {
        contentFrame.Navigate(typeof(SamplePage2));
    }
    else if (navSample.SelectedItem == SamplePage3Item)
    {
        contentFrame.Navigate(typeof(SamplePage3));
    }
    else if (navSample.SelectedItem == navSample.SettingsItem)
    {
        contentFrame.Navigate(typeof(SamplePage2));
    }

    if (contentFrame.CanGoBack)
    {
        navSample.IsBackEnabled = true;
    }
}
private void NavSample_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
{
    if (contentFrame.CanGoBack)
    {
        contentFrame.GoBack();

    }
}

也许这些图片可以帮助您更好地理解。

Using Navigation Menu to browse page

Using Navigation Menu to browse page (2)

Using back button and the expectation doesn't fulfill

您可以通过以下方式跟踪当前页面(SamplePage1、SamplePage2..等)并手动设置所选菜单项:

SamplePage1Item.IsSelected = true;

你可以这样写你的逻辑:

private void NavSample_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
{
    if (contentFrame.CanGoBack)
    {
        contentFrame.GoBack();

        // setting selected menu 
        if(contentFrame.Content is SamplePage1) SamplePage1Item.IsSelected = true;
        else if(contentFrame.Content is SamplePage2) SamplePage2Item.IsSelected = true;
    }
}

因为contentFrame的内容和选中的菜单之间没有link/relation。您要么以编程方式设置所选菜单,要么它会响应 tap/click.

而改变
Dictionary<string, Type> _pages =
        new()
        {
            { "Requests", typeof(Pages.Requests.Requests) },
            { "Shift", typeof(Pages.Shift.Shift) }
        };

    public void NavigationViewItemInvoked(NavigationView navigationView, NavigationViewItemInvokedEventArgs args)
    {
        NavigationViewItem item = (NavigationViewItem)args.InvokedItemContainer;

        if(
            _pages.TryGetValue(item.Name, out Type t) &&
            t != _contentFrame.CurrentSourcePageType)
            switch (args.IsSettingsInvoked)
            {
                case true:
                    _contentFrame.Navigate(typeof(Pages.Settings.Settings));
                    break;

                case false:
                    _contentFrame.Navigate(t);
                    break;
            }
    }

无论其价值如何,您也可以为设置添加字典条目。