首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Gjallarhorn处理多个命令(来自按钮)的正确方法

使用Gjallarhorn处理多个命令(来自按钮)的正确方法
EN

Stack Overflow用户
提问于 2017-08-07 18:38:46
回答 1查看 163关注 0票数 5

窗口上有几个按钮,我试图找到处理命令的好方法。

例如:

我得做些动作:

代码语言:javascript
复制
type Action = 
    |Show
    |Open
    |Input
    |Change
    |Statistic

将此转换为xaml将是:

代码语言:javascript
复制
<Button Command="{Binding ShowCommand}" />
<Button Command="{Binding OpenCommand}" />
<Button Command="{Binding InputCommand}" />
<Button Command="{Binding ChangeCommand}" />
<Button Command="{Binding StatisticCommand}" />

稍微玩一下库,我找到了两种不用烦人的冗长的方法。

  1. 使用Observable.merge
代码语言:javascript
复制
Binding.createMessage "StatisticCommand" Statistic source
|> Observable.merge (Binding.createMessage "InputCommand" Input source)
//|> Observable.merge ... and so on
|> Observable.subscribe (performAction model.Value)
|> source.AddDisposable
  1. 创建通用消息
代码语言:javascript
复制
type GeneralMessage = 
    |Perform of Action
    |Update of Message

并将行动信息提升到高层

代码语言:javascript
复制
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

(嗯,我喜欢第一种选择,因为这个动作不会改变模型)

是做这些事情的正确方式(第一种,显而易见的),还是库中包含更合适的功能?

我在找Observable.concat [info; stat; input; ...],但运气不好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-07 20:38:12

所以这两种选择都可以。我认为适当的方法取决于如何使用,以及需要什么数据:

  • 如果"Action“是组件应该完整处理的内容,则第一个选项是完全有效的。这将该组件的工作封装在该函数中,用于设置绑定,并将其完全排除在模型之外。
  • 如果"Action“需要模型以外的任何内容(或模型的可用部分),则向上传播(如选项2)是最有意义的。这允许模型处理操作,并对其进行适当处理。
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45553650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档