0

我尝试使用Microsoft.Toolkit.Mvvm为这个示例(只是简单的分页部分)实现 MVVM 模式,不幸的是,我失败了:(因为我在 WPF 中也很菜鸟 MVVM :))

主要问题是如何使用 InvokeCommandAction ( Microsoft.Xaml.Behaviors.Wpf ) 将事件的参数传递给命令?我认为文档和 wiki 有限......在这种情况下,我在MainWindow.xaml中更改此代码:

...
        <ui:Frame x:Name="ContentFrame" Navigated="ContentFrame_Navigated" />
    </ui:NavigationView>

至 :

            ...
<ui:Frame x:Name="ContentFrame" DataContext="{Binding ContentFrameVM}">
            <i:Interaction.Triggers>
                <!--  Events  -->
                <i:EventTrigger EventName="Navigated">
                    <i:InvokeCommandAction Command="{Binding ContentFrame_NavigatedCommand}" PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </ui:Frame>

并从MainWindow.xaml.cs移动与此事件相关的代码:

        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
    {
        if (e.SourcePageType() == typeof(SettingsPage))
        {
            NavView.SelectedItem = NavView.SettingsItem;
        }
        else
        {
            NavView.SelectedItem = NavView.MenuItems.OfType<NavigationViewItem>().FirstOrDefault(x => GetPageType(x) == e.SourcePageType());
        }
    }

到 MainWindowViewModel.cs 并将其更改为:

   class MainWindowViewModel : ObservableObject
  {
  public IRelayCommand ContentFrame_NavigatedCommand { get; }
  private NavigationView _navigationViewVM;
    public NavigationView NavigationViewVM
    {
        get => _navigationViewVM;
        set => SetProperty(ref _navigationViewVM, value);
    }

    private ModernWpf.Controls.Frame _contentFrameVM;
    public ModernWpf.Controls.Frame ContentFrameVM
    {
        get => _contentFrameVM;
        set => SetProperty(ref _contentFrameVM, value);
    }

    public MainWindowViewModel()
    {
        ContentFrame_NavigatedCommand = new RelayCommand<object>(ContentFrame_Navigated, (o) => { return true; });
    }
    ...

    private void ContentFrame_Navigated(object o)
    {
        NavigationEventArgs e = o as NavigationEventArgs;
        if (e.SourcePageType() == typeof(Views.Pages.SettingsPage))
        {
            NavigationViewVM.SelectedItem = NavigationViewVM.SettingsItem;
        }
        else
        {
            NavigationViewVM.SelectedItem = NavigationViewVM.MenuItems.OfType<NavigationViewItem>().FirstOrDefault(x => GetPageType(x) == e.SourcePageType());
        }
    }

或试试这个:

public MainWindowViewModel()
    {
        ContentFrame_NavigatedCommand = new RelayCommand<NavigationEventArgs>(ContentFrame_Navigated);
    }
...
private void ContentFrame_Navigated(NavigationEventArgs e)
    {
        ...
    }

在调试模式下,“ContentFrame_Navigated”根本不会触发,“ContentFrame_NavigatedCommand”只是在启动时触发一次(当时“NavigationViewVM”为空!)

我忽略了一个明显的问题不是吗?另外,这可能是重复的对不起,但我试图阅读所有类似的问题并参考了好几天!

4

1 回答 1

1

感谢 Jesse 的评论...我编辑了那部分代码,但主要问题是 ItemInvoked 事件根本没有实现!但是,我决定改为实现 SelectionChanged 事件: MainWindow.xaml :

<ui:NavigationView>
<i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged" SourceObject="{Binding ElementName=NavView}">
                <i:InvokeCommandAction Command="{Binding NavVM_SelectionChangedCommand}" PassEventArgsToCommand="True"/>
            </i:EventTrigger>
  </i:Interaction.Triggers>
   ...

在 MainWindowViewModel.cs 中:

 private void NavVM_SelectionChanged(NavigationViewSelectionChangedEventArgs args)
    {
        if (args.IsSettingsSelected)
        {
            Navigate(typeof(Views.Pages.SettingsPage));
        }
        else
        {
            var selectedItem = (NavigationViewItem)args.SelectedItem;
            Navigate(selectedItem);
        }
    }
于 2021-11-22T10:10:38.467 回答