C# 在ReactiveUI Windows窗体中将EventArgs传递给ReactiveCommand

C# 在ReactiveUI Windows窗体中将EventArgs传递给ReactiveCommand,c#,.net,winforms,events,reactiveui,C#,.net,Winforms,Events,Reactiveui,我正在将ReactiveUI与Windows窗体和c#一起使用。我不知道如何从ReactiveCommand中访问EventArgs 我的看法是: this.BindCommand(ViewModel, vm => vm.FileDragDropped, v => v.listViewFiles, nameof(listViewFiles.DragDrop)); 视图模型: FileDragDropped = ReactiveCommand.Create(() => {

我正在将ReactiveUI与Windows窗体和c#一起使用。我不知道如何从ReactiveCommand中访问EventArgs

我的看法是:

this.BindCommand(ViewModel, vm => vm.FileDragDropped, v => v.listViewFiles, nameof(listViewFiles.DragDrop));
视图模型:

FileDragDropped = ReactiveCommand.Create(() =>
{
    // Do something with DragEventArgs
    // Obtained from listViewFiles.DragDrop in View
});

如何从ReactiveComma和文件DragDropped中获取DragDrop事件参数?

您可以直接处理事件并将其传递给命令。例如,使用标准WPF中的标签并使用
ReactiveUI.Events
nuget包

var rc = ReactiveCommand.Create<DragEventArgs>
    ( e => Console.WriteLine( e ));

this.Events().Drop.Subscribe( e => rc.Execute( e ) );
var rc=ReactiveCommand.Create
(e=>Console.WriteLine(e));
this.Events().Drop.Subscribe(e=>rc.Execute(e));
或者,如果您想坚持使用XAML,那么就创建附加行为

public class DropCommand : Behavior<FrameworkElement>
{
    public ReactiveCommand<DragEventArgs,Unit> Command
    {
        get => (ReactiveCommand<DragEventArgs,Unit>)GetValue(CommandProperty);
        set => SetValue(CommandProperty, value);
    }

    // Using a DependencyProperty as the backing store for ReactiveCommand.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.Register("Command", typeof(ReactiveCommand<DragEventArgs,Unit>), typeof(DropCommand), new PropertyMetadata(null));


    // Using a DependencyProperty as the backing store for ReactiveCommand.  This enables animation, styling, binding, etc...


    private IDisposable _Disposable;


    protected override void OnAttached()
    {
        base.OnAttached();
        _Disposable = AssociatedObject.Events().Drop.Subscribe( e=> Command?.Execute(e));
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        _Disposable.Dispose();
    }
}
公共类DropCommand:行为
{
公共反应命令
{
get=>(ReactiveCommand)GetValue(CommandProperty);
set=>SetValue(CommandProperty,value);
}
//使用DependencyProperty作为ReactiveCommand的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty CommandProperty=
Register(“Command”、typeof(ReactiveCommand)、typeof(DropCommand)、newpropertyMetadata(null));
//使用DependencyProperty作为ReactiveCommand的后备存储。这将启用动画、样式、绑定等。。。
私人IDisposable(一次性);;
受保护的覆盖无效附加()
{
base.onatached();
_一次性=AssociatedObject.Events().Drop.Subscribe(e=>Command?.Execute(e));
}
附加时受保护的覆盖无效()
{
base.OnDetaching();
_一次性的,一次性的;
}
}
像这样使用它

<Label>
    <i:Interaction.Behaviors>
        <c:DropCommand Command="{Binding DropCommand}" />
    </i:Interaction.Behaviors>
</Label>


我在你发布的代码中没有看到
EventArgs
。你能发布一个@BradGonesSurfing的可能副本吗?这个问题与Windows窗体(不是WPF)有关。我使用的是Windows窗体(不是WPF),但第一个块也适用于Windows窗体。谢谢只需忽略XAML部分并使用直接事件绑定。如果答案对你有效,那么你应该将其标记为已接受。我尝试了你的附加行为,但无效。原来我必须在execute命令之后添加一个.Subscribe():
\u Disposable=AssociatedObject.Events().Drop.Subscribe(e=>{command?.execute(e.Subscribe();Debug.WriteLine(“删除了某些内容”);})