彩票走势图

logo DevExpress WinForm中文手册

Fluent API支持


立即下载DevExpress WinForms

Fluent APIs利用方法级联传递后续调用的指令上下文,通过这样做,Fluent API遵循与人们使用的相同的自然语言规则。因此,构造良好的Fluent API提供了更易于感知和理解的人性化代码。

要开始了解Fluent API概念,请参考以下文章。

  • 了解

一篇由Sacha Barber撰写的Code Project博客文章,描述了什么是Fluent API以及为什么应该使用它。

MSDN文档部分描述了实体框架范围内的Fluent API。

DevExpress MVVM框架提供了扩展方法来为任何任务构建流畅的API表达式:从绑定简单的属性到将MVVM行为与特定事件关联起来。

属性绑定和UI触发器

  • 简单的属性绑定。

C#:

mvvmContext.ViewModelType = typeof(ViewModel);
var fluentAPI = mvvmContext.OfType<ViewModel>();
fluentAPI.SetBinding(editor, e => e.EditValue, x => x.Title);
//ViewModel
public virtual string Title { get; set; }

VB.NET:

mvvmContext.ViewModelType = GetType(ViewModel)
Dim fluentAPI = mvvmContext.OfType(Of ViewModel)()
fluentAPI.SetBinding(editor, Function(e) e.EditValue, Function(x) x.Title)
'ViewModel
public virtual String Title {get;set;}
  • 绑定到嵌套属性。

C#:

mvvmContext.ViewModelType = typeof(ViewModel);
var fluent = mvvmContext.OfType<ViewModel>();
fluent.SetBinding(editor, e => e.EditValue, x => x.Child.Title);
//ViewModel
public NestedViewModel Child { get; private set; }
//NestedViewModel
public virtual string Title { get; set; }

VB.NET:

mvvmContext.ViewModelType = GetType(ViewModel)
Dim fluent = mvvmContext.OfType(Of ViewModel)()
fluent.SetBinding(editor, Function(e) e.EditValue, Function(x) x.Child.Title)
'ViewModel
public NestedViewModel Child {get;private set;}
'NestedViewModel
public virtual String Title {get;set;}
  • UI 触发器。

C#:

mvvmContext.ViewModelType = typeof(UIViewModel);
var fluentAPI = mvvmContext.OfType<UIViewModel>();
fluentAPI.SetTrigger(x => x.IsActive, (active) =>
{
label.Text = active ? "Active" : "Inactive";
});
//UIViewModel
public virtual bool IsActive { get; set; }

VB.NET:

mvvmContext.ViewModelType = GetType(UIViewModel)
Dim fluentAPI = mvvmContext.OfType(Of UIViewModel)()
fluentAPI.SetTrigger(Function(x) x.IsActive, Sub(active)
label.Text = If(active, "Active", "Inactive"))
'UIViewModel
public virtual Boolean IsActive {get;set;}

命令绑定

  • 带有CanExecute条件的参数化命令。

C#:

mvvmContext.ViewModelType = typeof(ViewModel);
int parameter = 4;
var fluentAPI = mvvmContext.OfType<ViewModel>();
fluentAPI.BindCommand(commandButton, (x, p) => x.DoSomething(p), x => parameter);

//ViewModel
public class ViewModel {
public void DoSomething(int p) {
//. . .
}

public bool CanDoSomething(int p) {
return (2 + 2) == p;
}
}

VB.NET:

mvvmContext.ViewModelType = GetType(ViewModel)
Dim parameter As Integer = 4
Dim fluentAPI = mvvmContext.OfType(Of ViewModel)()
fluentAPI.BindCommand(commandButton, Function(x, p) x.DoSomething(p), Function(x) parameter)

'ViewModel
public class ViewModel
public void DoSomething(Integer p)
'. . .

public Boolean CanDoSomething(Integer p)
Return (2 + 2) = p
  • 异步命令。

C#:

mvvmContext.ViewModelType = typeof(ViewModel);
var fluentAPI = mvvmContext.OfType<ViewModel>();
fluentAPI.BindCommand(commandButton, x => x.DoSomethingAsynchronously());
fluentAPI.BindCancelCommand(cancelButton, x => x.DoSomethingAsynchronously());

//ViewModel
public class ViewModelWithAsyncCommandAndCancellation
public Task DoSomethingAsynchronously() {
return Task.Factory.StartNew(() =>
{
var asyncCommand = this.GetAsyncCommand(x => x.DoSomethingAsynchronously());
for(int i = 0; i <= 100; i++) {
if(asyncCommand.IsCancellationRequested) // cancellation check
break;
//. . .
}
});
}
}

VB.NET:

mvvmContext.ViewModelType = GetType(ViewModel)
Dim fluentAPI = mvvmContext.OfType(Of ViewModel)()
fluentAPI.BindCommand(commandButton, Function(x) x.DoSomethingAsynchronously())
fluentAPI.BindCancelCommand(cancelButton, Function(x) x.DoSomethingAsynchronously())

'ViewModel
public class ViewModelWithAsyncCommandAndCancellation public Task DoSomethingAsynchronously()
Return Task.Factory.StartNew(Sub() ' cancellation check
'. . .
Dim asyncCommand = Me.GetAsyncCommand(Function(x) x.DoSomethingAsynchronously())
For i As Integer = 0 To 100
If asyncCommand.IsCancellationRequested Then
Exit For
End If
Next i
End Sub)
}
  • WithCommand扩展允许您将命令绑定到一个或多个目标UI元素。

C#:

//binding to one UI element
fluent.WithCommand(x => x.DoSomething())
.Bind(btnDoSomething);
//binding to multiple UI elements
fluent.WithCommand(x => x.DoSomething())
.Bind(btn1DoSomething)
.Bind(btn2DoSomething);
fluent.WithCommand(x => x.DoSomethingAsynchronously())
.Bind(btnDo)
.BindCancel(btnCancel);

VB.NET:

'binding to one UI element
fluent.WithCommand(Function(x) x.DoSomething()).
Bind(btnDoSomething)
'binding to multiple UI elements
fluent.WithCommand(Function(x) x.DoSomething())
.Bind(btn1DoSomething)
.Bind(btn2DoSomething)
fluent.WithCommand(Function(x) x.DoSomethingAsynchronously())
.Bind(btnDo)
.BindCancel(btnCancel)
  • 命令触发器允许您在目标命令执行之前、之后或该命令的CanExecute条件发生变化时自动调用特定的方法。

C#:

fluent.WithCommand(x => x.DoSomething())
.After(() => AfterDoSomethingExecuted());
fluent.WithCommand(x => x.DoSomething())
.Before(() => BeforeDoSomethingExecuted());
fluent.WithCommand(x => x.DoSomething())
.OnCanExecuteChanged(() => WhenCanDoSomethingChanged());

VB.NET:

fluent.WithCommand(Function(x) x.DoSomething())
.After(Function() AfterDoSomethingExecuted())
fluent.WithCommand(Function(x) x.DoSomething())
.Before(Function() BeforeDoSomethingExecuted())
fluent.WithCommand(Function(x) x.DoSomething())
.OnCanExecuteChanged(Function() WhenCanDoSomethingChanged())

附加操作

  • 确认操作 。

C#:

mvvmContext.WithEvent<ChangingEventArgs>(editor, "EditValueChanging")
.Confirmation(behavior =>
{
behavior.Caption = "CheckEdit State changing";
behavior.Text = "This checkEdit's checked-state is about to be changed. Are you sure?";
});

VB.NET:

mvvmContext.WithEvent(Of ChangingEventArgs)(editor, "EditValueChanging").Confirmation(Sub(behavior)
behavior.Caption = "CheckEdit State changing"
behavior.Text = "This checkEdit's checked-state is about to be changed. Are you sure?"
End Sub)
  • Event-To-Command操作。

C#:

mvvmContext.ViewModelType = typeof(ViewModel);
mvvmContext.WithEvent<ViewModel, EventArgs>(thirdPartyButton, "Click")
.EventToCommand(x => x.DoSomething());

//ViewModel
public void DoSomething() {
//. . .
}

VB.NET:

mvvmContext.ViewModelType = GetType(ViewModel)
mvvmContext.WithEvent(Of ViewModel, EventArgs)(thirdPartyButton, "Click").EventToCommand(Function(x) x.DoSomething())

'ViewModel
Public Sub DoSomething()
'. . .
End Sub
  • 键到命令和键到命令操作。

C#:

mvvmContext.OfType<KeyAwareViewModel>()
.WithKey(memo, Keys.A)
.KeyToCommand(x => x.OnAKey());
mvvmContext.OfType<KeyAwareViewModel>()
.WithKeys(memo, new Keys[] { Keys.A, Keys.B, Keys.C })
.KeysToCommand(x => x.OnKey(Keys.None), args => args.KeyCode);

VB.NET:

mvvmContext.OfType(Of KeyAwareViewModel)().WithKey(memo, Keys.A).KeyToCommand(Function(x) x.OnAKey())
mvvmContext.OfType(Of KeyAwareViewModel)().WithKeys(memo, New Keys() { Keys.A, Keys.B, Keys.C }).KeysToCommand(Function(x) x.OnKey(Keys.None), Function(args) args.KeyCode)
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP