【问题标题】:F# Event driven MVVM using WPF使用 WPF 的 F# 事件驱动 MVVM
【发布时间】:2015-07-14 13:23:17
【问题描述】:

我已经与Event driven MVVM 合作了几个星期(第一次在 F# 中使用设计模式),我喜欢分离视图和模型以及“功能”控制器的想法。但是在阅读一本关于 WPF 的书时,我感觉如果我可以直接处理事件会更容易。此外,在某些情况下,我需要从后面的代码中获取控件。

更具体:

  • 如何关闭在 XAML 文件中定义为用户控件的窗口
  • 如果我可以直接处理事件,那么对按钮(触发保持状态的布尔值)的需求似乎会减少,因此会有更自动化的感觉

有人分享这种经验还是我仍然错过了什么?是否建议返回 FsXaml 或 polyglot MVVM?

【问题讨论】:

    标签: wpf mvvm f#


    【解决方案1】:

    原来代码实际上很容易扩展。基于demos,我能够将文本框变成数字文本框。执行此操作的代码非常基本,但我的意图是定义自定义事件访问器。可以通过以下方式完成:

    扩展 UserControl.xaml 标头:

    xmlns:fsxaml="http://github.com/fsprojects/FsXaml"   
    fsxaml:ViewController.Custom="{x:Type views:CompositionUserControl}"
    

    并替换UserControl.xaml.fs中的原代码:

    namespace Space.Views
    
    open FsXaml
    
    type UserView = XAML<"View/UserControl.xaml", true>
    
    type CompositionUserControl () =
    
        member __.ViewModel = Space.ViewModels.UserControlViewModel(Space.Models.Handling.proces)
    

    namespace Space.Views
    
    open FsXaml
    open System
    
    type UserView = XAML<"View/UserControl.xaml", true>
    
    type CompositionUserControl () =
        inherit UserControlViewController<UserView>()
    
        let numeric (txt : string) =
            try txt |> int with
            | :? System.FormatException -> 0
            | _ -> 1
    
        override this.OnLoaded view = 
            view.Box.PreviewTextInput.Add(fun e -> if numeric e.Text = 0 then e.Handled <- true) 
    
        member __.ViewModel = Space.ViewModels.UserControlViewModel(Space.Models.Handling.proces)
    

    编辑

    回顾这篇文章,这是我最初提出问题的进展:

    如何关闭在 XAML 文件中定义为用户控件的窗口

    使用附加属性DialogCloser

    似乎不再需要按钮(触发布尔值 保持状态),如果可以的话,结果会更加自动化 直接处理事件

    这里的关键是学习:

    1. 如何真正将视图(模型)从模型中分离出来
    2. 如何使用 XAML 发挥其全部功能

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 2011-04-22
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多