C# XAML 为 NavigationView 内的不同 MenuItem 设置命令 [英] C# XAML Set Commands for different MenuItems inside a NavigationView

查看:18
本文介绍了C# XAML 为 NavigationView 内的不同 MenuItem 设置命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xaml 文件,我在其中设置了带有 MenuItems 的 NavigationView.在我的项目中,我实现了一个基本的导航服务,它可以让我在命令触发器上切换帧:

I have a xaml file where I set up a NavigationView with MenuItems. Inside my Project I implemented a basic Navigationservice which lets me switch the frames on Command Trigger:

导航服务:

    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 是一个基本的实现,你到处都能找到.

Now an example Command would be this: The RelayCommand is a basic implementation that you find all over the place.

        private ICommand _navigateToTextToSpeechView;

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

进一步,我通过 DataContext 在视图的代码隐藏文件中分配 ViewModel.

Further on I assign the ViewModel inside the code behind file of the view through the DataContext.

<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 的答案,但现在该命令会针对我不想要的所有 MenuItem 触发.我想为不同的 MenuItem 分配不同的命令.我如何才能做到这一点?

Now I found an answer to how to assign a command to the MenuItems thorugh the Microsoft.Xaml.Behaviors.Uwp.Managed NuGet package but the command gets now triggered for all MenuItems which I dont want. I want to assign different Commands to different MenuItems. How would I be able to achieve this?

推荐答案

我是这样解决的:

        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.

Personally I dont like this approach but it works. Just if someone is interested. If someone coould provide a better solution without string checks than I would be happy. In the long run this function could become pretty big depending on how much NavigationMenuItems I have.

这篇关于C# XAML 为 NavigationView 内的不同 MenuItem 设置命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆