我在Windows窗体和c#中使用ReactiveUI。我不确定如何从ReactiveCommand中访问EventArgs。

我的看法:

this.BindCommand(ViewModel, vm => vm.FileDragDropped, v => v.listViewFiles, nameof(listViewFiles.DragDrop));


ViewModel:

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


如何从ReactiveComma和FileDragDropped中获取DragDrop EventArgs?

最佳答案

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

var rc = ReactiveCommand.Create<DragEventArgs>
    ( 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();
    }
}


并像这样使用

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

关于c# - 在ReactiveUI Windows窗体中将EventArgs传递给ReactiveCommand,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43313289/

10-10 04:35