To jeden z najwilgotniejszych kodów, jakie napisałem. Ale jest to przydatne, co jest denerwujące. Powodem wszystkich powtórzeń jest to, że chcę zachować płynność interfejsu. Gdybym zwiększona klasę bazową (co zdarza się View
w tym przypadku), to tylko dać z powrotem wystąpienie View
, które pozwalają mi robić coś podobnegoJak mogę WYSUSZYĆ ten kod F #? (Płynny interfejs)
let label = theme.CreateLabel().WithMargin(new Thickness(5.0)).WithText("Hello")
ponieważ nieruchomość Label.Text
nie jest realizowany przez klasa podstawowa View
.
Oto mój płynny interfejs. Przygotuj się. Jest brzydki i powtarzalny. Ale działa również i jest wygodny w użyciu.
Czy przegapiłem oczywisty sposób, aby to WYSOKIE?
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|])
UPDATE
Więc dzięki waszej pomocy, odpowiedź brzmi: tak, brakowało mi coś oczywistego. Jak wyjaśniono TheQuickBrownFox, jeśli zmienię biegle interfejs do czegoś formularza
let label = theme.CreateLabel() |> withMargin(new Thickness(5.0)) |> withContent("Hello")
wtedy potwór widać powyżej mogą zostać zastąpione w całości przez
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|])
Skreślenie kod jest bardzo miłe rzeczywiście.
nie można zrobić aplikacja postfix (inaczej "pipe forward") zamiast metod rozszerzenia? W ten sposób możesz uczynić funkcje ogólnymi z ograniczeniami. –
Muszę przyznać, nie byłem do końca pewien, co miałeś na myśli, dopóki nie przeczytałem odpowiedzi TheQuickBrownFox. Teraz ma to sens. I działa bardzo ładnie. Aktualizacja oczekująca. –