本文共 2116 字,大约阅读时间需要 7 分钟。
今天说一下协同IResult
看一下IResult接口
////// Allows custom code to execute after the return of a action. /// public interface IResult { ////// Executes the result using the specified context. /// /// The context. void Execute(ActionExecutionContext context); ////// Occurs when execution has completed. /// event EventHandlerCompleted; }
Execute方法里写你要执行的事件,在最后执行事件Completed这是一定要执行的,不然会无法执行后继的yield部分
Execute 方法有一个ActionExecutionContext参数,这个参数与建立UI相关的IResult实现中
非常有用。它能提供的功能如下
public class ActionExecutionContext{ public ActionMessage Message; public FrameworkElement Source; public object EventArgs; public object Target; public DependencyObject View; public MethodInfo Method; public FuncCanExecute; public object this[string key];}
Message: 造成这 IResult 的调用原始 ActionMessage。
Source: FrameworkElement 触发执行的行动。
EventArgs: 与行动的触发器相关联的任何事件参数。
Target: 在实际的操作方法存在的类实例。
View: 与目标关联的视图。
Method: MethodInfo 指定要在目标实例上调用的方法。
CanExecute: 一个函数,如果操作可被调用、 虚假否则返回 true。
key index: 一个地方来存储/检索它可以对框架的扩展所使用的任何附加元数据。
做一个小Demo
源码:
由于这个例子很简单我们把bootstrapper也写简单一些
class HelloBootstrapper : Bootstrapper{ }
这样就可以 了
新建一下Loader类去实现IResult接口public class Loader : IResult { readonly string _str; public Loader(string str) { _str = str; } public void Execute(ActionExecutionContext context) { MessageBox.Show(_str + context.View); Completed(this, new ResultCompletionEventArgs());//这个方法一定要加到这里,这个方法完成后才会执行后边的方法 } public event EventHandlerCompleted = (sender, args) => { MessageBox.Show(((Loader)sender)._str ); }; }
在ViewModel里我们看一下它的方法实现
public IEnumerableMyIResultClick() { yield return new Loader("load....."); yield return new Loader("Ok!"); }
源码:
本文转自lpxxn博客园博客,原文链接:http://www.cnblogs.com/li-peng/p/3440972.html,如需转载请自行联系原作者