[WPF] Caliburn Micro学习三 Binding

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)" />
上一篇:LeetCode 204 Count Primes


下一篇:Spring IOC学习(3)——FactoryBeanRegistrySupport