【问题标题】:Pass event argument to command with Microsoft.Toolkit.Mvvm and Microsoft.Xaml.Behaviors.Wpf使用 Microsoft.Toolkit.Mvvm 和 Microsoft.Xaml.Behaviors.Wpf 将事件参数传递给命令
【发布时间】:2021-12-18 06:17:30
【问题描述】:

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

主要问题是如何使用 InvokeCommandAction (Microsoft.Xaml.Behaviors.Wpf) 将事件的参数传递给命令?我认为文档和维基有限... 在这种情况下,我在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”为空!)

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

【问题讨论】:

  • 您的命令位于MainWindowViewModel,但您将DataContext 设置为ContentFrameVM?绑定引擎正在您的ContentFrameVM 上查找名为ContentFrame_Navigated 的命令。

标签: c# wpf mvvm data-binding eventtocommand


【解决方案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);
        }
    }

【讨论】:

    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2010-11-06
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2015-06-09
    相关资源
    最近更新 更多