日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Caliburn笔记-基本Command(wpf框架)

發布時間:2025/5/22 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.使用容器

首先定義一個對象

注意點:

  • 掛上Command標簽
  • 方法必須為Execute和CanExecute
  • [Command] public class ShowMessageCommand {//Note: A command must have an entry point. A method named 'Execute' is searched for by default. //Note: Use the CommandAttribute.ExecuteMethod to specify a different method.//Note: The 'Execute' method inherits all the features available to actions.public void Execute(string message){MessageBox.Show(message);}//NOTE: This is picked up automatically by the ActionFactory based on its naming convention.public bool CanExecute(string message){return !string.IsNullOrEmpty(message);} }


    xaml

    <Button Content="Attached Container Command w/ 1 Parameter" cal:Message.Attach="ContainerCommand ShowMessage(message.Text)"/>


    注意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);} }


    定義資源

    <Window.Resources><!--Note: Adding a command to resources.--><local:ShowTitledMessageCommand x:Key="ShowTitledMessage" /> </Window.Resources>


    xaml引用

    <Button Content="Attached Resource Command w/ 2 Parameters"cal:Message.Attach="ResourceCommand ShowTitledMessage(title.Text, message.Text)" />

    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)" />


    我們通過代碼來看下不同的解析過程

    protected override void SetCore(CommandMessage message, DependencyObject target, string coreOfMessage){switch(_commandSource){case CommandSource.Resource:var frameworkElement = target as FrameworkElement;if(frameworkElement != null){frameworkElement.Loaded +=delegate { message.Command = frameworkElement.GetResource<object>(coreOfMessage); };} break;case CommandSource.Container:message.Command = ServiceLocator.Current.GetInstance(null, coreOfMessage);break;case CommandSource.Bound:var binding = new Binding(coreOfMessage);BindingOperations.SetBinding(message, CommandMessage.CommandProperty, binding); break;default:throw new NotSupportedException(_commandSource + " is not a supported command source.");}}


    實質上是非常簡單的,主要手段還是通過給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框架)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。