窗口上有几个按钮,我试图找到处理命令的好方法。
例如:
我必须执行一些操作:
type Action =
|Show
|Open
|Input
|Change
|Statistic
将其转换为 xaml 将是:
<Button Command="{Binding ShowCommand}" />
<Button Command="{Binding OpenCommand}" />
<Button Command="{Binding InputCommand}" />
<Button Command="{Binding ChangeCommand}" />
<Button Command="{Binding StatisticCommand}" />
有点玩图书馆我找到了两种方法来做到这一点而不会烦人的冗长
1. 使用Observable.merge
Binding.createMessage "StatisticCommand" Statistic source
|> Observable.merge (Binding.createMessage "InputCommand" Input source)
//|> Observable.merge ... and so on
|> Observable.subscribe (performAction model.Value)
|> source.AddDisposable
2. 创建泛化消息
type GeneralMessage =
|Perform of Action
|Update of Message
并将行动信息提升到高层
let mainComponent source (model : ISignal<Info>) =
let info = Binding.componentToView source "Info" infoComponent model
//...
let stat = Binding.createMessage "StatCommand" (Perform Statistic) source
let input = Binding.createMessage "InputCommand" (Perform Input) source
//let ...
[info; stat; input; ...]
let appComponent =
let model = initInfo
let update message model =
match message with
|Update message ->
match message with
|...
|Perform action ->
performAction model action
model
Framework.basicApplication model update mainComponent
(嗯,我喜欢第一个选项,因为这个动作不会改变模型)
做这些事情是正确的方法(第一个,很明显)还是库包含更合适的功能?
PS我寻找Observable.concat [info; stat; input; ...]
但没有幸运。