You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI中EventToCommandBehavior如何在命令内获取Sender与EventArgs?

解决方案

要在RelayCommand中同时获取Editor实例、TestQuestion对象和TextChangedEventArgs,可以通过自定义附加行为实现,摆脱第三方工具包的行为限制,步骤如下:


1. 替换原有附加行为类

重新实现TestEditorStyleBehavior,直接处理TextChanged事件并封装所有需要的参数:

using Xamarin.Forms;

public class TestEditorStyleBehavior : Behavior<Editor>
{
    // 定义用于绑定视图模型命令的附加属性
    public static readonly BindableProperty CommandProperty = BindableProperty.CreateAttached(
        propertyName: "Command",
        returnType: typeof(ICommand),
        declaringType: typeof(TestEditorStyleBehavior),
        defaultValue: null);

    public static ICommand GetCommand(BindableObject view)
    {
        return (ICommand)view.GetValue(CommandProperty);
    }

    public static void SetCommand(BindableObject view, ICommand value)
    {
        view.SetValue(CommandProperty, value);
    }

    protected override void OnAttachedTo(Editor editor)
    {
        base.OnAttachedTo(editor);
        // 绑定TextChanged事件
        editor.TextChanged += OnEditorTextChanged;
    }

    protected override void OnDetachingFrom(Editor editor)
    {
        base.OnDetachingFrom(editor);
        // 解绑事件,避免内存泄漏
        editor.TextChanged -= OnEditorTextChanged;
    }

    private void OnEditorTextChanged(object sender, TextChangedEventArgs e)
    {
        var editor = sender as Editor;
        if (editor == null) return;

        var command = GetCommand(editor);
        if (command == null) return;

        // 封装所有需要的参数:Editor实例、TestQuestion、事件参数
        var commandParam = new EditorTextChangedArgs
        {
            Sender = editor,
            Question = editor.BindingContext as TestQuestion,
            EventArgs = e
        };

        if (command.CanExecute(commandParam))
        {
            command.Execute(commandParam);
        }
    }
}

// 定义强类型参数类,避免使用dynamic类型
public class EditorTextChangedArgs
{
    public Editor Sender { get; set; }
    public TestQuestion Question { get; set; }
    public TextChangedEventArgs EventArgs { get; set; }
}

2. 修改Style配置

移除原有的EventToCommandBehavior,直接绑定自定义附加行为的Command属性:

<Style x:Key="SpacesQuestionEditor" TargetType="Editor">
    <Setter Property="behaviors:TestEditorStyleBehavior.Command"
            Value="{Binding Source={x:Reference TestEditorPage}, Path=BindingContext.EditorTextChangedCommand}" />
</Style>

3. 更新视图模型的命令方法

修改RelayCommand的参数类型为自定义的EditorTextChangedArgs,即可直接获取所有需要的对象:

[RelayCommand]
public void EditorTextChanged(EditorTextChangedArgs args)
{            
   // 保留原有业务逻辑
   ChangeHtml(args.Question);         

   // 获取Editor实例
   var editor = args.Sender;
   // 获取文本变化前后的值
   var oldText = args.EventArgs.OldTextValue;
   var newText = args.EventArgs.NewTextValue;
}

4. 保留原有Editor代码

原Editor代码无需修改,保持原样即可:

<Editor x:Name="spacesTextEditor" Placeholder="Enter text" Style="{StaticResource SpacesQuestionEditor}" Text="{Binding SpacesEditorText}" AutoSize="TextChanges"/>

内容的提问来源于stack exchange,提问作者Death_Dealer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 04:14:54