【发布时间】: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 触发。我想将不同的命令分配给不同的菜单项。我怎样才能做到这一点?
【问题讨论】: