Caliburn.Micro學(xué)習(xí)筆記目錄
今天說(shuō)一下協(xié)同IResult
看一下IResult接口

/// <summary> /// Allows custom code to execute after the return of a action. /// </summary> public interface IResult { /// <summary> /// Executes the result using the specified context. /// </summary> /// <param name="context">The context.</param> void Execute(ActionExecutionContext context); /// <summary> /// Occurs when execution has completed. /// </summary> event EventHandler<ResultCompletionEventArgs> Completed; }
Execute方法里寫你要執(zhí)行的事件,在最后執(zhí)行事件Completed這是一定要執(zhí)行的,不然會(huì)無(wú)法執(zhí)行后繼的yield部分
Execute 方法有一個(gè)ActionExecutionContext參數(shù),這個(gè)參數(shù)與建立UI相關(guān)的IResult實(shí)現(xiàn)中
非常有用。它能提供的功能如下

public class ActionExecutionContext{ public ActionMessage Message; public FrameworkElement Source; public object EventArgs; public object Target; public DependencyObject View; public MethodInfo Method; public Func<bool> CanExecute; public object this[string key];}
Message: 造成這 IResult 的調(diào)用原始 ActionMessage。
Source: FrameworkElement 觸發(fā)執(zhí)行的行動(dòng)。
EventArgs: 與行動(dòng)的觸發(fā)器相關(guān)聯(lián)的任何事件參數(shù)。
Target: 在實(shí)際的操作方法存在的類實(shí)例。
View: 與目標(biāo)關(guān)聯(lián)的視圖。
Method: MethodInfo 指定要在目標(biāo)實(shí)例上調(diào)用的方法。
CanExecute: 一個(gè)函數(shù),如果操作可被調(diào)用、 虛假否則返回 true。
key index: 一個(gè)地方來(lái)存儲(chǔ)/檢索它可以對(duì)框架的擴(kuò)展所使用的任何附加元數(shù)據(jù)。
做一個(gè)小Demo
源碼:CaliburnIresult.rar
由于這個(gè)例子很簡(jiǎn)單我們把bootstrapper也寫簡(jiǎn)單一些
class HelloBootstrapper : Bootstrapper<MyViewModel> { }這樣就可以 了新建一下Loader類去實(shí)現(xiàn)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());//這個(gè)方法一定要加到這里,這個(gè)方法完成后才會(huì)執(zhí)行后邊的方法 } public event EventHandler<ResultCompletionEventArgs> Completed = (sender, args) => { MessageBox.Show(((Loader)sender)._str ); }; }
前臺(tái)我們就放一下button就可以

<Window x:Class="CaliburnIresult.MyView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/PResentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:cal="http://www.caliburnproject.org" Title="MyView" Height="300" Width="300"> <Grid> <Button Content="IResult" cal:Message.Attach="MyIResultClick"/> </Grid></Window>
在ViewModel里我們看一下它的方法實(shí)現(xiàn)
public IEnumerable<IResult> MyIResultClick() { yield return new Loader("load....."); yield return new Loader("Ok!"); } 源碼:CaliburnIresult.rar
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注