反应性扩展(Rx)+ MVVM =? [英] Reactive Extensions (Rx) + MVVM =?

查看:70
本文介绍了反应性扩展(Rx)+ MVVM =?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于解释Reactive Extensions(Rx)功能的主要示例之一是将现有的鼠标事件组合到一个新的事件"中,该事件表示鼠标拖动期间的增量:

One of the main examples being used to explain the power of Reactive Extensions (Rx) is combining existing mouse events into a new 'event' representing deltas during mouse drag:

var mouseMoves = from mm in mainCanvas.GetMouseMove()
                 let location = mm.EventArgs.GetPosition(mainCanvas)
                 select new { location.X, location.Y};

var mouseDiffs = mouseMoves
    .Skip(1)
    .Zip(mouseMoves, (l, r) => new {X1 = l.X, Y1 = l.Y, X2 = r.X, Y2 = r.Y});

var mouseDrag = from _  in mainCanvas.GetMouseLeftButtonDown()
                from md in mouseDiffs.Until(
                    mainCanvas.GetMouseLeftButtonUp())
                select md;

来源: Matthew Podwysocki的反应性框架简介"系列.

在MVVM中,我通常会努力保持.xaml.cs文件尽可能为空,而纯粹使用标记的viewmodel中的命令从视图挂接事件的一种方法是使用行为:

In MVVM I generally strive to keep my .xaml.cs file as empty as possible and one way of hooking up events from the view with commands in the viewmodel purely in markup is using a behavior:

<Button Content="Click Me">
    <Behaviors:Events.Commands>
        <Behaviors:EventCommandCollection>
            <Behaviors:EventCommand CommandName="MouseEnterCommand" EventName="MouseEnter" />
            <Behaviors:EventCommand CommandName="MouseLeaveCommand" EventName="MouseLeave" />
            <Behaviors:EventCommand CommandName="ClickCommand" EventName="Click" />
        </Behaviors:EventCommandCollection>
    </Behaviors:Events.Commands>
</Button>

来源: Brian Genisio .

Reactive Framework似乎更适合于传统的MVC模式,在这种模式下,控制器知道视图并可以直接引用其事件.

The Reactive Framework seems to be more geared towards the traditional MVC pattern where a controller knows the view and can reference its events directly.

但是,我想既要吃蛋糕又要吃!

But, I want to both have my cake and eat it!

您将如何结合这两种模式?

How would you combine these two patterns?

推荐答案

我编写了一个框架,表示我对这个问题的探索,称为 ReactiveUI

I've written a framework that represents my explorations in this question called ReactiveUI

它既实现了Observable ICommand,又实现了通过IObservable发出更改信号的ViewModel对象,还实现了将IObservable分配"给属性的能力,然后,只要其IObservable发生更改,IobifyPropertyChange便会触发INotifyPropertyChange.它还封装了许多常见的模式,例如让ICommand在后台运行Task,然后将结果编组回UI.

It implements both an Observable ICommand, as well as ViewModel objects who signal changes via an IObservable, as well as the ability to "assign" an IObservable to a property, who will then fire INotifyPropertyChange whenever its IObservable changes. It also encapsulates a lot of common patterns, like having an ICommand who runs a Task in the background, then marshalls the result back to the UI.

现在我的文档绝对为零,但是我将在未来几天内努力添加这些信息,以及我已经编写的示例应用程序

I have absolutely zero documentation up right now, but I'll be working on adding that information over the coming days, as well as a sample application I've coded up

更新:我现在有很多文档,请查阅 http://www .reactiveui.net

UPDATE: I now have quite a lot of documentation up, check out http://www.reactiveui.net

这篇关于反应性扩展(Rx)+ MVVM =?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆