Caliburn笔记-基本Command(wpf框架)
生活随笔
收集整理的這篇文章主要介紹了
Caliburn笔记-基本Command(wpf框架)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考:http://caliburn.codeplex.com/wikipage?title=Command%20Basics&referringTitle=Documentation??
command在wpf的viewmodel中使用的非常的廣泛,討論的也非常多.
來看下caliburn對command的支持
caliburn支持三種方式command的注冊
- 容器
- 資源
- 綁定
Command解析字符串
parser.RegisterMessageParser("ResourceCommand", new CommandMessageParser(binder, CommandSource.Resource)); parser.RegisterMessageParser("ContainerCommand", new CommandMessageParser(binder, CommandSource.Container)); parser.RegisterMessageParser("BoundCommand", new CommandMessageParser(binder, CommandSource.Bound));為簡化xaml,在Attach中采用了特定解析的字符串,下面可以看到
1.使用容器
首先定義一個對象
注意點:
xaml
注意xaml使用方式,ContainerCommand為特定的解析字符串ShowMessage則代表ShowMessageCommand,以下規則是一樣的
2.使用資源
首先然后定義對象,注意Preview標簽
public class ShowTitledMessageCommand {//Note: A command must have a method named 'Execute'//Note: The 'Execute' method inherits all the features available to actions.[Preview("CanExecute")]public void Execute(string title, string message){MessageBox.Show(message, title);}public bool CanExecute(string title, string message){return !string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(message);} }
定義資源
xaml引用
3.使用綁定
要綁定的前提是需要有源
首先要定義源
<Window.DataContext><!--Note: Makes the presentation model available for binding through the data context.--><local:MyModel /> </Window.DataContext>使用BoundCommand綁定
<TextBox x:Name="message" /><!--Note: Executes the command located through data binding. The framework infers the trigger type.--> <Button Content="Click Me!"cal:Message.Attach="BoundCommand TheCommand(message.Text)" />
我們通過代碼來看下不同的解析過程
實質上是非常簡單的,主要手段還是通過給CommandMessage的Command賦值,方式不同而已.以上三種方式一比較,第一種是最簡單的,這也是依賴注入帶來的好處,不需要事先顯示聲明對象.效果都是一樣的
來看下原始的用法
<Button Content="Triggers: Container Command With 1 Explicit Parameters"><cal:Message.Triggers><cal:RoutedMessageTriggerCollection><cal:EventMessageTrigger EventName="Click"><cal:EventMessageTrigger.Message><!--Note ResolveExtension doesn't exist in Silverlight, but you can auto resolve by string key. See SL samples.--><cal:CommandMessage Command="{cal:Resolve Key=ShowMessage}"><!--Note: The declaration of parameters is different from Silverlight.--><cal:Parameter Value="{Binding ElementName=message, Path=Text}"/></cal:CommandMessage></cal:EventMessageTrigger.Message></cal:EventMessageTrigger></cal:RoutedMessageTriggerCollection></cal:Message.Triggers> </Button>轉載于:https://www.cnblogs.com/Clingingboy/archive/2009/12/29/1634712.html
總結
以上是生活随笔為你收集整理的Caliburn笔记-基本Command(wpf框架)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux下的nfs服务配置
- 下一篇: end_form_tag 已经在rail