我在我的应用程序中使用 NavigationView 作为主控件,并在页面加载时使用框架。
<NavigationView x:Name="MyNavView" IsBackButtonVisible="Collapsed" SelectionChanged="{x:Bind ViewModel.OnSelectionChanged}" PaneDisplayMode="Top">
<NavigationView.MenuItems>
<NavigationViewItem Icon="Contact" Content="Contact" Tag="MasterDetailPage"/>
<NavigationViewItem Icon="Favorite" Content="Favorites" Tag="FavoritesPage"/>
</NavigationView.MenuItems>
<Frame x:Name="RootFrame"/>
</NavigationView>
有两个事件选择已更改 and 调用的项目可以实现导航到加载的页面根框架(我的框架的名称)。但我想用Command来制作MVVM。我什至还没有找到 Command 道具导航视图本身或为了导航视图项。之后我在 ViewModel 中处理了 SelectionChanged 事件,但在我看来它与 MVVM 相矛盾。
那么,如何使用 Command 来制作 MVVM?如果没有这样的机会告诉我如何实现MVVM本身不处理事件。
实现这一点与 WPF 的实现方式非常相似,您需要首先通过 NuGet 安装 Microsoft.Xaml.Behaviors.Uwp.Managed 包。然后向您的 NavigationView 添加一个行为:
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
<NavigationView MenuItemsSource="{x:Bind ViewModel.MenuItems}">
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="ItemInvoked">
<core:EventTriggerBehavior.Actions>
<core:InvokeCommandAction Command="{x:Bind ViewModel.ItemInvokedCommand}" />
</core:EventTriggerBehavior.Actions>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
我在用着x:Bind
这里用于编译时错误检查,但常规Binding
当然也会同样有效。无论哪种方式,都可以在视图模型中使用命令处理程序来跟进,就像处理 WPF 一样:
private ICommand _ItemInvokedCommand;
public ICommand ItemInvokedCommand => this._ItemInvokedCommand ?? (this._ItemInvokedCommand = new RelayCommand<NavigationViewItemInvokedEventArgs>(OnItemInvoked));
private void OnItemInvoked(NavigationViewItemInvokedEventArgs args)
{
// could also use a converter on the command parameter if you don't like
// the idea of passing in a NavigationViewItemInvokedEventArgs
this.NavigationService.NavigateTo(args.InvokedItem);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)