WPF C# MVVM 社区工具包中继命令在自己的文件夹中

WPF C# MVVM Community Toolkit Relay Commands in own folder

我正在尝试使用 Microsoft MVVM 社区工具包实施中继命令。如果我将命令代码放在 ViewModel 中,它就可以工作。如果我尝试将命令代码放在其自己的文件夹中,则无法找到命令代码。 我在下面创建了一个非常简单的示例,在 ViewModel 中发布命令代码(已注释掉),并尝试将命令 ccode 放在它自己的文件夹中。我怀疑我在文件夹中执行的代码都是错误的。我没有在其自己的文件夹中找到使用中继命令的命令代码示例。我可以使用 ICommand 和 CommandBase 将命令代码放在它自己的文件夹中。

App.xaml.cs

using RelayCommandLocation.Views;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace RelayCommandLocation
{
   /// <summary>
   /// Interaction logic for App.xaml
   /// </summary>
   public partial class App : Application
   {
      protected override void OnStartup(StartupEventArgs e)
      {
         base.OnStartup(e);
         RelayCommandView relayCommandView = new RelayCommandView();
         relayCommandView.DataContext = new RelayCommandViewModel();
         relayCommandView.Show();
      }
   }
   
}

文件夹 ViewModels 中的 ViewModel

using Microsoft.Toolkit.Mvvm.Input;
using RelayCommandLocation.Commands;
using System.Windows.Input;

namespace RelayCommandLocation.ViewModels
{
   public class RelayCommandViewModel : ObservableObject
   {
      public ICommand PushMeClick { get; set; }

      // ################################################################################
      // ############# This fails to compile = cannot find PushMeCommand code


      public RelayCommandViewModel()
      {
         PushMeClick = new RelayCommand(PushMeCommandX.PushMeCommand);
      }


      // ********************************************************************************
      // *********** This works. The RelayCommand code is in the ViewModel. *************

      //public RelayCommandViewModel()
      //{
      //   PushMeClick = new RelayCommand(PushMeCommand);
      //}

      //private void PushMeCommand()
      //{
      //   MessageBox.Show($"Button pushed");
      //}
   }
}

文件夹命令中的命令代码

using System;
using System.Windows;
using System.Windows.Input;

namespace RelayCommandLocation.Commands
{
   public class PushMeCommandX 
   {
      public void PushMeCommand()
      {
         MessageBox.Show($"Button pushed");
      }
   }
}

XAML 在文件夹 Views

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:RelayCommandLocation.Views"
        mc:Ignorable="d"
        Title="RelayCommandView" Height="450" Width="800">
    <Grid>
        <Button Content="Push Me" Command="{Binding PushMeClick}" Height="50" Width="75" />
    </Grid>
</Window>

是否可以将中继命令代码放在其自己的文件夹中?

根据@Klaus Gutter 的评论,您需要在 RelayCommandViewModel

中创建 PushMeCommandX 的实例
using Microsoft.Toolkit.Mvvm.Input;
using RelayCommandLocation.Commands;
using System.Windows.Input;

namespace RelayCommandLocation.ViewModels
{
   public class RelayCommandViewModel : ObservableObject
   {
      public ICommand PushMeClick { get; set; }
      // Create property or a field for your PushMeCommandX class.
      public PushMeCommandX PushMe {get; set;}

      public RelayCommandViewModel()
      {
         // Create an instance of the PushMeCommandX class to get access to it's methods.
         PushMe = new PushMeCommandX();
         PushMeClick = new RelayCommand(PushMe.PushMeCommand);
      }
    }
}

我会和@Sir Rufo 一起阅读一些关于 C# 的 Tutorial 类。