Fluent API利用方法级联来连接后续调用的指令上下文。通过这样做,Fluent API遵循与人们使用的自然语言规则相同的自然语言规则。因此,构造良好的Fluent API提供了更人性化的代码,更容易理解和理解。
提示: 要想着手了解Fluent API概念,请参阅以下文章。看一看Fluent api 由Sacha Barber写的一篇代码项目博客文章,描述了什么是Fluent API以及为什么你应该使用它。第一行Fluent API代码 MSDN文档里的一章,它描述了Entity Framework范围内的Fluent API。
DevExpress MVVM框架提供了扩展方法来为任何任务构建连贯的API 表达式 :从绑定简单的属性到将MVVM行为与特定事件关联。
mvvmContext.ViewModelType = typeof(ViewModel);var fluentAPI = mvvmContext.OfType<ViewModel>();fluentAPI.SetBinding(editor, e => e.EditValue, x => x.Title);//ViewModel.cspublic virtual string Title { get; set; }mvvmContext.ViewModelType = typeof(ViewModel);var fluent = mvvmContext.OfType<ViewModel>();fluent.SetBinding(editor, e => e.EditValue, x => x.Child.Title);//ViewModel.cspublic NestedViewModel Child { get; private set; }//NestedViewModel.cspublic virtual string Title { get; set; }mvvmContext.ViewModelType = typeof(UIViewModel);var fluentAPI = mvvmContext.OfType<UIViewModel>();fluentAPI.SetTrigger(x => x.IsActive, (active) =>{ label.Text = active ? "Active" : "Inactive";});//UIViewModel.cspublic virtual bool IsActive { get; set; }mvvmContext.ViewModelType = typeof(ViewModel);int parameter = 4;var fluentAPI = mvvmContext.OfType<ViewModel>();fluentAPI.BindCommand(commandButton, (x, p) => x.DoSomething(p), x => parameter); //ViewModelpublic class ViewModel { public void DoSomething(int p) { //. . . } public bool CanDoSomething(int p) { return (2 + 2) == p; }}mvvmContext.ViewModelType = typeof(ViewModel);var fluentAPI = mvvmContext.OfType<ViewModel>();fluentAPI.BindCommand(commandButton, x => x.DoSomethingAsynchronously());fluentAPI.BindCancelCommand(cancelButton, x => x.DoSomethingAsynchronously()); //ViewModelpublic 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; //. . . } }); }}//绑定到一个UI元素fluent.WithCommand(x => x.DoSomething()) .Bind(btnDoSomething);//绑定到多个UI元素fluent.WithCommand(x => x.DoSomething()) .Bind(btn1DoSomething) .Bind(btn2DoSomething);fluent.WithCommand(x => x.DoSomethingAsynchronously()) .Bind(btnDo) .BindCancel(btnCancel);fluent.WithCommand(x => x.DoSomething()) .After(() => AfterDoSomethingExecuted());fluent.WithCommand(x => x.DoSomething()) .Before(() => BeforeDoSomethingExecuted());fluent.WithCommand(x => x.DoSomething()) .OnCanExecuteChanged(() => WhenCanDoSomethingChanged());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?"; });mvvmContext.ViewModelType = typeof(ViewModel);mvvmContext.WithEvent<ViewModel, EventArgs>(thirdPartyButton, "Click") .EventToCommand(x => x.DoSomething()); //ViewModelpublic void DoSomething() { //. . .}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);
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删