- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以这是我写过的最潮湿的代码。但它很有用,这很烦人。所有重复的原因是因为我想保持界面流畅。如果我扩充基类(在这种情况下恰好是 View
),它只会返回 View
的实例,这将阻止我做类似的事情
let label = theme.CreateLabel().WithMargin(new Thickness(5.0)).WithText("Hello")
Label.Text
View
没有实现属性基类。
module ViewExtensions =
let private withTwoWayBinding<'TElement, 'TProperty, 'TViewModel, 'TView when 'TView :> IViewFor<'TViewModel>>(viewModel: 'TViewModel, view: 'TView, viewModelProperty: Expr<'TViewModel -> 'TProperty>, viewProperty: Expr<'TView -> 'TProperty>) (element: 'TElement) =
view.Bind(viewModel, ExpressionConversion.toLinq viewModelProperty, ExpressionConversion.toLinq viewProperty) |> ignore
element
let private withHorizontalOptions<'TElement when 'TElement :> View> options (element: 'TElement) =
element.HorizontalOptions <- options
element
let private withVerticalOptions<'TElement when 'TElement :> View> options (element: 'TElement) =
element.VerticalOptions <- options
element
let private withAlignment<'TElement when 'TElement :> View> horizontalOptions verticalOptions (control: 'TElement) =
control |> withHorizontalOptions horizontalOptions |> withVerticalOptions verticalOptions
let private withMargin<'TElement when 'TElement :> View> margin (element: 'TElement) =
element.Margin <- margin
element
let private withActions<'TElement> (actions: ('TElement -> unit)[]) (element: 'TElement) =
for action in actions do action(element)
element
type Xamarin.Forms.Entry with
member this.WithHorizontalOptions(options) = withHorizontalOptions options this
member this.WithVerticalOptions(options) = withHorizontalOptions options this
member this.WithAlignment(horizontalOptions, verticalOptions) = withAlignment horizontalOptions verticalOptions this
member this.WithTwoWayBinding(viewModel, view, viewModelProperty, viewProperty) = withTwoWayBinding(viewModel, view, viewModelProperty, viewProperty) this
member this.WithMargin(margin) = withMargin margin this
member this.With(actions) = withActions actions this
member this.With(action: Entry -> unit) = this.With([|action|])
type Xamarin.Forms.Grid with
member this.WithHorizontalOptions(options) = withHorizontalOptions options this
member this.WithVerticalOptions(options) = withHorizontalOptions options this
member this.WithAlignment(horizontalOptions, verticalOptions) = withAlignment horizontalOptions verticalOptions this
member this.WithMargin(margin) = withMargin margin this
member this.With(actions) = withActions actions this
member this.With(action: Grid -> unit) = this.With([|action|])
type Xamarin.Forms.StackLayout with
member this.WithHorizontalOptions(options) = withHorizontalOptions options this
member this.WithVerticalOptions(options) = withHorizontalOptions options this
member this.WithAlignment(horizontalOptions, verticalOptions) = withAlignment horizontalOptions verticalOptions this
member this.WithMargin(margin) = withMargin margin this
member this.With(actions) = withActions actions this
member this.With(action: StackLayout -> unit) = this.With([|action|])
type Xamarin.Forms.Button with
member this.WithHorizontalOptions(options) = withHorizontalOptions options this
member this.WithVerticalOptions(options) = withHorizontalOptions options this
member this.WithAlignment(horizontalOptions, verticalOptions) = withAlignment horizontalOptions verticalOptions this
member this.WithMargin(margin) = withMargin margin this
member this.WithText(text) = this.Text <- text; this
member this.With(actions) = withActions actions this
member this.With(action: Button -> unit) = this.With([|action|])
type Xamarin.Forms.Switch with
member this.WithHorizontalOptions(options) = withHorizontalOptions options this
member this.WithVerticalOptions(options) = withHorizontalOptions options this
member this.WithAlignment(horizontalOptions, verticalOptions) = withAlignment horizontalOptions verticalOptions this
member this.WithTwoWayBinding(viewModel, view, viewModelProperty, viewProperty) = withTwoWayBinding(viewModel, view, viewModelProperty, viewProperty) this
member this.WithMargin(margin) = withMargin margin this
member this.With(actions) = withActions actions this
member this.With(action: Switch -> unit) = this.With([|action|])
type Xamarin.Forms.Label with
member this.WithHorizontalOptions(options) = withHorizontalOptions options this
member this.WithVerticalOptions(options) = withHorizontalOptions options this
member this.WithAlignment(horizontalOptions, verticalOptions) = withAlignment horizontalOptions verticalOptions this
member this.WithMargin(margin) = withMargin margin this
member this.WithText(text) = this.Text <- text; this
member this.With(actions) = withActions actions this
member this.With(action: Label -> unit) = this.With([|action|])
let label = theme.CreateLabel() |> withMargin(new Thickness(5.0)) |> withContent("Hello")
module ViewExtensions =
let withTwoWayBinding<'TElement, 'TProperty, 'TViewModel, 'TView when 'TView :> IViewFor<'TViewModel>>(viewModel: 'TViewModel, view: 'TView, viewModelProperty: Expr<'TViewModel -> 'TProperty>, viewProperty: Expr<'TView -> 'TProperty>) (element: 'TElement) =
view.Bind(viewModel, ExpressionConversion.toLinq viewModelProperty, ExpressionConversion.toLinq viewProperty) |> ignore
element
let withHorizontalOptions options (element: #View) = element.HorizontalOptions <- options; element
let withVerticalOptions options (element: #View) = element.VerticalOptions <- options; element
let withAlignment horizontalOptions verticalOptions element = element |> withHorizontalOptions horizontalOptions |> withVerticalOptions verticalOptions
let withMargin margin (element: #View) = element.Margin <- margin; element
let withCaption text (element: #Button) = element.Text <- text; element
let withText text (element: #Entry) = element.Text <- text; element
let withContent text (element: #Label) = element.Text <- text; element
let withSetUpActions<'TElement> (actions: ('TElement -> unit)[]) (element: 'TElement) = (for action in actions do action(element)); element
let withSetUpAction<'TElement> (action: 'TElement -> unit) = withSetUpActions([|action|])
最佳答案
流畅接口(interface)的惯用 F# 方法就是使用管道转发运算符 |>
module ViewHelpers
let withMargin margin element = ...
let withText text element = ...
open ViewHelpers
let label = theme.CreateLabel() |> withMargin (new Thickness(5.0)) |> withText "Hello"
let withMargin margin (element: #View) = ...
关于f# - 我怎样才能干掉这个 F# 代码? (流畅的界面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38724189/
最近在造轮子,从 0 到 1 的那种,就差前台的界面了,大家可以耐心耐心耐心期待一下。其中需要设计一些数据库表,可以通过 Navicat 这种图形化管理工具直接开搞,也可以通过一些数据库设计工具来搞,
本文已经收录到Github仓库,该仓库包含 计算机基础、Java基础、多线程、JVM、数据库、Redis、Spring、Mybatis、SpringMVC、SpringBoot、分布式、微服务、设
干燥路径“/A”和“/B”的解析参数的“Angular”方式是什么?在我的 $routeProvider 中,我为这些场景调用完全相同的解析函数,并且不知道避免在 AngularJS 框架中重复代码的
我确信有一种方法可以 DRY 代码。 changeColor() 所做的只是改变父组件的背景颜色。 import { Play } from "./play"; import { Hello } fr
我在 Node v6.3.0 中的 api 上运行以下代码,该 api 运行两个单独的 Promise,具体取决于 POST 请求中是否存在参数的条件。 if (paramExists) {
在我的 Ruby on Rails 项目中,我有一个邮件程序,它基本上为给定用户准备系统中发生的事情的每日摘要。在邮件 Controller 中,我根据一些通用模式(在特定日期内、不是由该用户创作
我是一名优秀的程序员,十分优秀!