【问题标题】:C# XAML Set Commands for different MenuItems inside a NavigationViewC# XAML 为 NavigationView 中的不同菜单项设置命令
【发布时间】:2021-09-07 03:36:31
【问题描述】:

我有一个 xaml 文件,我在其中设置了带有 MenuItems 的 NavigationView。在我的项目中,我实现了一个基本的 Navigationservice,它可以让我在 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 是一个基本的实现,你可以在任何地方找到。

        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 触发。我想将不同的命令分配给不同的菜单项。我怎样才能做到这一点?

【问题讨论】:

    标签: c# xaml mvvm uwp


    【解决方案1】:

    我是这样解决的:

            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。

    【讨论】:

    • 当我点击按钮时,它说我明天可以将其标记为已解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2015-08-27
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多