C# XAML 为 NavigationView 中的不同菜单项设置命令

C# XAML Set Commands for different MenuItems inside a NavigationView

我有一个 xaml 文件,我在其中设置了带有 MenuItems 的 NavigationView。在我的项目中,我实现了一个基本的导航服务,它允许我在命令触发器上切换框架:

导航服务:

    public class NavigationService : INavigationService
    {
        public void GoBack()
        {
            var frame = (Frame)Window.Current.Content;
            frame.GoBack();
        }

        public void Navigate(Type sourcePage)
        {
            var frame = (Frame)Window.Current.Content;
            frame.Navigate(sourcePage);    
        }

        public void Navigate(Type sourcePage, object parameter)
        {
            var frame = (Frame)Window.Current.Content;
            frame.Navigate(sourcePage, parameter);
        }

        public void NavigateScrollViewer(Type sourcePage)
        {
            //ToDo Inject sourcePage into ScrollViewer of Frame
            var frame = (Frame)Window.Current.Content;
            var page = frame.CurrentSourcePageType;
        }
    }

现在一个示例命令是这样的:RelayCommand 是一个基本的实现,随处可见。

        private ICommand _navigateToTextToSpeechView;

        public ICommand NavigateToTextToSpeechView
        {
            get 
            {
                return _navigateToTextToSpeechView =
                    new RelayCommand((a) =>
                    {
                        _navigationService.Navigate(typeof(AudioTextToSpeechView));
                        //ScrollFrame.Navigate(typeof(AudioHomeViewModel));
                    });
            }
        }

此外,我通过 DataContext 在视图的代码隐藏文件中分配了 ViewModel。

<Page
    x:Class="ToolBoxApp.Views.AudioHomeView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ToolBoxApp.Views"
    xmlns:viewmodels="using:ToolBoxApp.ViewModels"
    xmlns:mainview="clr-namespace:ToolBoxApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:core="using:Microsoft.Xaml.Interactions.Core"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <!--<Page.DataContext>
        <viewmodels:AudioHomeViewModel/>
    </Page.DataContext>-->
    
    <Grid>
        <NavigationView x:Name="navigationViewControl" 
                        IsBackEnabled="true">

            <i:Interaction.Behaviors>
                <core:EventTriggerBehavior EventName="ItemInvoked">
                    <core:EventTriggerBehavior.Actions>
                        <core:InvokeCommandAction Command="{Binding NavigateToTextToSpeechView}" />
                    </core:EventTriggerBehavior.Actions>
                </core:EventTriggerBehavior>
            </i:Interaction.Behaviors>

            <NavigationView.MenuItems>
                <NavigationViewItem Icon="MusicInfo" Content="Text to Speech"/>
                <NavigationViewItem Icon="MusicInfo" Content="Youtube to Mp3"/>
            </NavigationView.MenuItems>

            <ScrollViewer>
                <Frame x:Name="ContentFrame"/>
            </ScrollViewer>
    
        </NavigationView>
    </Grid>
</Page>

现在我找到了如何通过 Microsoft.Xaml.Behaviors.Uwp.Managed NuGet 包将命令分配给 MenuItems 的答案,但是现在会为我不想要的所有 MenuItems 触发该命令。我想将不同的命令分配给不同的菜单项。我怎样才能做到这一点?

我是这样解决的:

        public ICommand NavigateToTextToSpeechView
        {
            get 
            {
                return _navigateToTextToSpeechView =
                    new GenericRelayCommand<NavigationViewItemInvokedEventArgs>(OnItemInvoked);
            }
        }

        public void OnItemInvoked(NavigationViewItemInvokedEventArgs args)
        {
            string invokedItemName = args.InvokedItem.ToString();
            Debug.WriteLine(invokedItemName);

            if (invokedItemName.Equals("Text to Speech"))
            {
                _navigationService.Navigate(typeof(AudioTextToSpeechView));
            }
        }

我个人不喜欢这种方法,但它确实有效。只要有人感兴趣。 如果有人可以在不进行字符串检查的情况下提供更好的解决方案,我会很高兴。从长远来看,运行 这个功能可能会变得相当大,具体取决于我有多少 NavigationMenuItems。