[WPF] Caliburn Micro学习三 Binding

原创 2013年12月02日 23:41:39

Caliburn Micro学习一 Installation 

Caliburn Micro学习二 Infrastructure


如果说WPF推崇的Binding开辟了一条UI开发新的方式——让写代码的人专注在代码上,让界面设计师去实现界面,他们工作在同一个项目上,使用不同的IDE,最终Build出来真正的产品,那Caliburn Micro无疑是一个加速器,它不仅加速了Binding的使用范畴,还加速了代码的编写速度。

首先,得介绍另外一个Assembly:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"


这个Assembly包含在Express Blend SDK里面,Microsoft Download Page:http://www.microsoft.com/en-us/download/details.aspx?id=3062


没有Caliburn.Micro,绑定到一个Event到一个ICommand

        public ICommand CommandDataPathSelectionChanged
        {
            get
            {
                if (_cmd_path_selchged == null)
                    _cmd_path_selchged = new ACRelayCommand(
                        x => OnDataPathSelectionChanged(x)
                            );

                return _cmd_path_selchged;
            }
        }
其中,ACRelayCommand的定义:

    public class ACRelayCommand : ICommand
    {
        #region Fields
        readonly Action<object> _execute;
        readonly Predicate<object> _canExecute;
        #endregion // Fields

        #region Constructors
        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public ACRelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public ACRelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }
        #endregion // Constructors

        #region ICommand Members
        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            _execute(parameter);
        }
        #endregion // ICommand Members
    }


而作为对比,使用了Caliburn.Micro,可以直接绑定Event到一个Method

            <ComboBox Name="cmbDataPath" Width="485" ItemsSource="{Binding DataPathList}" 
                      SelectedItem="{Binding SelectedDataPath}" >
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <cal:ActionMessage MethodName="OnSelectionChanged">
                            <cal:Parameter Value="$source" />
                            <cal:Parameter Value="$eventArgs" />
                        </cal:ActionMessage>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>

其中,Parameter的Value可以指定如下值:

  • $eventArgs – EventArgs对象.
  • $dataContext – UI界面空间的DataContext.
  • $source – 实际FrameworkElement.
  • $view - 绑定在ViewModel的View.
  • $executionContext - 执行上下文.
  • $this - UIelement 对象.

另外,常用的XAML中绑定语法:

<Button Content="DoSomething"
        cal:Message.Attach="[Event Click] = [Action DoSomething($dataContext)]" />
甚至同时绑定多个Event:

<Button Content="Let's Talk"
        cal:Message.Attach="[Event MouseEnter] = [Action Talk('Hello', Name.Text)]; [Event MouseLeave] = [Action Talk('Goodbye', Name.Text)]" />

简化的语法:

<Button Content="Click Me"
        cal:Message.Attach="SayHello(Name)" />







版权声明:© 2004 - 2016 Alva Chien, All Rights Reserved.

相关文章推荐

Caliburn Micro Part 2: Data Binding and Events

In my previous blog post I showed you how to get started with using the Caliburn Micro Framework i...

Caliburn.Micro with WPF TabControl

WPF Caliburn.Micro and TabControl with UserControls issue up vote3down votefavorite 5 ...

解析Caliburn.Micro(三)

书接前文,前篇文章简略了介绍了一下Caliburn.Micro(简称CM)的Action,这篇文章继续讨论CM的下一个Feature:Convention。 什么是Convention  ...

Caliburn.Micro学习笔记(二)----Actions

Caliburn.Micro学习笔记目录 上一篇已经简单说了一下引导类和简单的控件绑定 我的上一个例子里的button自动匹配到ViewModel事件你一定感觉很好玩吧 今天说一下它...

Caliburn.Micro学习笔记(五)----协同IResult

Caliburn.Micro学习笔记目录 今天说一下协同IResult 看一下IResult接口 /// /// Allows custom code to exec...

Caliburn.Micro

  • 2012-11-05 19:55
  • 6.62MB
  • 下载

Coproject - a RIA Caliburn.Micro demo, part 10

In this part, we will add a busy indicator. As you know, communication with server takes some tim...

Coproject - a RIA Caliburn.Micro demo, part 13

By now, you are probably using Caliburn.Micro for your applications and hopefully Coproject helped y...

Caliburn Micro Part 1: Getting Started

A few days ago I blogged about our new Dashboard sample included in WPF Elements. The sample demon...

AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(菜单篇)

这章主要说插件的菜单,可以说菜单是最核心的部分,前面我们已经实现了Document添加,现在主要就是生成具有层级关系的菜单,以及把菜单跟我们自定义的Document关联起来,也就是MenuPart->...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)