gpt4 book ai didi

wpf - 使用 WPF 的 F# 事件驱动 MVVM

转载 作者:行者123 更新时间:2023-12-04 16:12:44 25 4
gpt4 key购买 nike

我一直在使用 Event driven MVVM几个星期(第一次在 F# 中使用设计模式),我喜欢分离 View 和模型以及“功能” Controller 的想法。但是在阅读有关 WPF 的书籍时,我觉得如果我可以直接处理事件会更容易。此外,在某些情况下,我需要从隐藏的代码中获取控件。

更具体:

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

有没有人分享过这种经历,或者我是否还遗漏了什么?返回到 FsXaml 或多语言 MVVM 是否可取?

最佳答案

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

扩展 UserControl.xaml header :

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)

编辑

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

How to close a window defined as usercontrol in a XAML file

使用附加属性 DialogCloser .

It seems there would be less need for buttons (triggering booleans that hold state), with a more automated feel as result, if I could directly adress events

这里的关键是学习:

  1. 如何真正将 View (模型)与模型分开
  2. 如何使用 XAML 发挥其全部功能

关于wpf - 使用 WPF 的 F# 事件驱动 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31407930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com