Ev_*_*per 5 wpf f# gjallarhorn
窗口上有几个按钮,我试图找到处理命令的好方法.
例如:
我必须执行一些操作:
type Action =
|Show
|Open
|Input
|Change
|Statistic
Run Code Online (Sandbox Code Playgroud)
将其翻译为xaml将是:
<Button Command="{Binding ShowCommand}" />
<Button Command="{Binding OpenCommand}" />
<Button Command="{Binding InputCommand}" />
<Button Command="{Binding ChangeCommand}" />
<Button Command="{Binding StatisticCommand}" />
Run Code Online (Sandbox Code Playgroud)
有点玩图书馆我找到了两种方法来做到这一点,而没有烦人的冗长
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
Run Code Online (Sandbox Code Playgroud)
2.创建概括消息
type GeneralMessage =
|Perform of Action
|Update of Message
Run Code Online (Sandbox Code Playgroud)
并将行动信息提升到高水平
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
Run Code Online (Sandbox Code Playgroud)
(好吧,我喜欢第一个选项,导致这个动作不改变模型)
是正确的方式(第一个,显而易见的)做这些事情或库包含更多的拟合功能?
PS我寻找Observable.concat [info; stat; input; ...]但是没有幸运.
所以这两种选择都没关系.我认为适当的方法取决于如何使用它,以及需要什么数据:
如果"Action"是整个组件应该处理的东西,则第一个选项是完全有效的.这将该组件的工作封装在该函数中以设置绑定,并将其完全保留在模型之外.
如果"动作"需要模型之外的任何东西(或模型的可用部分),那么像选项2那样向上传播最有意义.这允许模型使用操作,并适当地处理它.