C# 如何将命令处理程序移动到另一个文件

C# 如何将命令处理程序移动到另一个文件,c#,wpf,xaml,C#,Wpf,Xaml,我在表单中找到了一个自定义路由命令示例,它运行良好 void ColorCmdExecuted(对象发送方,ExecutedRoutedEventArgs e),void ColorCmdCanExecute(对象发送方,CanExecuteRouteEventArgs e)在main window.cs中定义 如果我将这两个处理程序移动到xxxx.cs,如何更改XAML 编辑,添加更多信息 命令处理程序是在MainWindow.cs中定义的,我将代码剪切并粘贴到另一个文件中,如下所示,然后

我在表单中找到了一个自定义路由命令示例,它运行良好


void ColorCmdExecuted(对象发送方,ExecutedRoutedEventArgs e)
void ColorCmdCanExecute(对象发送方,CanExecuteRouteEventArgs e)
main window.cs中定义

如果我将这两个处理程序移动到xxxx.cs,如何更改XAML

编辑,添加更多信息

命令处理程序是在MainWindow.cs中定义的,我将代码剪切并粘贴到另一个文件中,如下所示,然后编译出错错误CS1061“MainWindow”不包含“ColorCmdExecuted”的定义。

//xxxx.cs
名称空间CustomRoutedCommand
{
公共类xxxx
{
//为自定义颜色命令执行RoutedEventHandler。
private void ColorCmdExecuted(对象发送方,ExecutedRoutedEventArgs e)
{
var目标=e.源作为面板;
如果(目标!=null)
{
target.Background=target.Background==brusks.AliceBlue?brusks.LemonChiffon:brusks.AliceBlue;
}
}
//自定义颜色命令的CanExecuteRouteEventHandler。
私有void ColorCmdCanExecute(对象发送方,CanExecuteRouteEventArgs e)
{
如果(例如,源为面板)
{
e、 CanExecute=true;
}
其他的
{
e、 CanExecute=false;
}
}
}
}

您可以继续使用
静态
命令,但更常见的是使用名为
RelayCommand
ICommand
实现。这使我们能够更容易地利用MVVM,以便您的视图模型能够处理这些命令。下面是
ICommand
的一个基本示例实现:

public class RelayCommand : ICommand
{
    private Predicate<object> _canExecute;
    private Action<object> _execute;

    public RelayCommand(Predicate<object> canExecute, Action<object> execute)
    {
        _canExecute = canExecute;
        _execute = execute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }
}

有关MVVM和ICommand实现的更多信息,请参阅大量参考资料。对于WPF初学者来说,这很容易理解,应该会让您对如何继续操作有更多的了解。

您可以继续使用
静态
命令,但更常见的是使用名为
RelayCommand
ICommand
实现。这使我们能够更容易地利用MVVM,以便您的视图模型能够处理这些命令。下面是
ICommand
的一个基本示例实现:

public class RelayCommand : ICommand
{
    private Predicate<object> _canExecute;
    private Action<object> _execute;

    public RelayCommand(Predicate<object> canExecute, Action<object> execute)
    {
        _canExecute = canExecute;
        _execute = execute;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }
}

有关MVVM和ICommand实现的更多信息,请参阅大量参考资料。对于WPF初学者来说,这很容易理解,应该让您对如何继续有更多的了解。

您不能将在XAML标记中连接的acual事件处理程序移动到另一个类,但您可以在另一个类中实现逻辑:

public partial class MainWindow : Window
{
    public static RoutedCommand ColorCmd = new RoutedCommand();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        xxxx.ColorCmdExecuted(e.Source);
    }

    private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = xxxx.ColorCmdCanExecute(e.Source);
    }
}

public class xxxx
{
    public static void ColorCmdExecuted(object parameter)
    {
        var target = parameter as Panel;
        if (target != null)
        {
            target.Background = target.Background == Brushes.AliceBlue ? Brushes.LemonChiffon : Brushes.AliceBlue;
        }
    }

    public static bool ColorCmdCanExecute(object parameter)
    {
        return parameter is Panel;
    }
}
您可能希望将
RoutedCommand
替换为
ICommand
接口的自定义实现,该接口可以在直接执行命令时执行某些操作:

public class RelayCommand : ICommand
{
    private Action<object> _execute;
    private Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
            return true;
        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }
}

public partial class MainWindow : Window
{
    public static RelayCommand ColorCmd = new RelayCommand(xxxx.ColorCmdExecuted, null);

    public MainWindow()
    {
        InitializeComponent();
    }
}

public class xxxx
{
    public static void ColorCmdExecuted(object parameter)
    {
        var target = parameter as Panel;
        if (target != null)
        {
            target.Background = target.Background == Brushes.AliceBlue ? Brushes.LemonChiffon : Brushes.AliceBlue;
        }
    }
}
公共类RelayCommand:ICommand
{
私人行动——执行;
私有谓词_canExecute;
公共RelayCommand(操作执行,谓词canExecute)
{
_执行=执行;
_canExecute=canExecute;
}
公共事件处理程序CanExecuteChanged;
公共布尔CanExecute(对象参数)
{
如果(_canExecute==null)
返回true;
返回_canExecute(参数);
}
public void Execute(对象参数)
{
如果(_execute!=null)
_执行(参数);
}
}
公共部分类主窗口:窗口
{
public static RelayCommand ColorCmd=新的RelayCommand(xxxx.ColorCmdExecuted,null);
公共主窗口()
{
初始化组件();
}
}
公共类xxxx
{
已执行公共静态void ColorCmdExecuted(对象参数)
{
var目标=作为面板的参数;
如果(目标!=null)
{
target.Background=target.Background==brusks.AliceBlue?brusks.LemonChiffon:brusks.AliceBlue;
}
}
}
XAML:

<Button Command="{x:Static local:MainWindow.ColorCmd}" Content="CommandTarget = FristStackPanel" />


有关该概念的更多信息,请参阅博客文章。

您不能将在XAML标记中连接的acual事件处理程序移动到另一个类,但可以在另一个类中实现该逻辑:

public partial class MainWindow : Window
{
    public static RoutedCommand ColorCmd = new RoutedCommand();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void ColorCmdExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        xxxx.ColorCmdExecuted(e.Source);
    }

    private void ColorCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = xxxx.ColorCmdCanExecute(e.Source);
    }
}

public class xxxx
{
    public static void ColorCmdExecuted(object parameter)
    {
        var target = parameter as Panel;
        if (target != null)
        {
            target.Background = target.Background == Brushes.AliceBlue ? Brushes.LemonChiffon : Brushes.AliceBlue;
        }
    }

    public static bool ColorCmdCanExecute(object parameter)
    {
        return parameter is Panel;
    }
}
您可能希望将
RoutedCommand
替换为
ICommand
接口的自定义实现,该接口可以在直接执行命令时执行某些操作:

public class RelayCommand : ICommand
{
    private Action<object> _execute;
    private Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        _execute = execute;
        _canExecute = canExecute;
    }
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        if (_canExecute == null)
            return true;
        return _canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        if (_execute != null)
            _execute(parameter);
    }
}

public partial class MainWindow : Window
{
    public static RelayCommand ColorCmd = new RelayCommand(xxxx.ColorCmdExecuted, null);

    public MainWindow()
    {
        InitializeComponent();
    }
}

public class xxxx
{
    public static void ColorCmdExecuted(object parameter)
    {
        var target = parameter as Panel;
        if (target != null)
        {
            target.Background = target.Background == Brushes.AliceBlue ? Brushes.LemonChiffon : Brushes.AliceBlue;
        }
    }
}
公共类RelayCommand:ICommand
{
私人行动——执行;
私有谓词_canExecute;
公共RelayCommand(操作执行,谓词canExecute)
{
_执行=执行;
_canExecute=canExecute;
}
公共事件处理程序CanExecuteChanged;
公共布尔CanExecute(对象参数)
{
如果(_canExecute==null)
返回true;
返回_canExecute(参数);
}
public void Execute(对象参数)
{
如果(_execute!=null)
_执行(参数);
}
}
公共部分类主窗口:窗口
{
public static RelayCommand ColorCmd=新的RelayCommand(xxxx.ColorCmdExecuted,null);
公共主窗口()
{
初始化组件();
}
}
公共类xxxx
{
已执行公共静态void ColorCmdExecuted(对象参数)
{
var目标=作为面板的参数;
如果(目标!=null)
{
target.Background=target.Background==brusks.AliceBlue?brusks.LemonChiffon:brusks.AliceBlue;
}
}
}
XAML:

<Button Command="{x:Static local:MainWindow.ColorCmd}" Content="CommandTarget = FristStackPanel" />


有关此概念的更多信息,请参阅博客帖子。

您确定要使用
静态
命令吗?问题是你能把它移到wt吗?不确定,我是WPF的初学者。我接受任何更好的建议。以及