c# WPF中System.Windows.Interactivity的使用
背景
在我們進行WPF開發(fā)應用程序的時候不可避免的要使用到事件,很多時候沒有嚴格按照MVVM模式進行開發(fā)的時候習慣直接在xaml中定義事件,然后再在對應的.cs文件中直接寫事件的處理過程,這種處理方式寫起來非常簡單而且不用過多地處理考慮代碼之間是否符合規(guī)范,但是我們在寫代碼的時候如果完全按照WPF規(guī)范的MVVM模式進行開發(fā)的時候就應該將相應的事件處理寫在ViewModel層,這樣整個代碼才更加符合規(guī)范而且層次也更加清楚,更加符合MVVM規(guī)范。
常規(guī)用法
1 引入命名空間
通過在代碼中引入System.Windows.Interactivity.dll,引入了這個dll后我們就能夠使用這個里面的方法來將事件映射到ViewModel層了,我們來看看具體的使用步驟,第一步就是引入命名控件
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
另外還可以通過另外一種方式來引入命名空間,其實這兩者間都是對等的。
xmlns:i=http://schemas.microsoft.com/expression/2010/interactivity
2 添加事件對應的Command
這里以TextBox的GetFocus和LostFocus為例來進行說明
<TextBox Text="CommandBinding">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<i:InvokeCommandAction Command="{Binding OnTextLostFocus}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type TextBox}}}"/>
</i:EventTrigger>
<i:EventTrigger EventName="GotFocus">
<i:InvokeCommandAction Command="{Binding OnTextGotFocus}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type TextBox}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
這個里面我們重點來看看這個InvokeCommandAction的代碼結構
namespace System.Windows.Interactivity
{
public sealed class InvokeCommandAction : TriggerAction<DependencyObject>
{
public static readonly DependencyProperty CommandProperty;
public static readonly DependencyProperty CommandParameterProperty;
public InvokeCommandAction();
public string CommandName { get; set; }
public ICommand Command { get; set; }
public object CommandParameter { get; set; }
protected override void Invoke(object parameter);
}
}
這里我們發(fā)現(xiàn)這里我們如果我們定義一個Command的話我們只能夠在Command中獲取到我們綁定的CommandParameter這個參數(shù),但是有時候我們需要獲取到觸發(fā)這個事件的RoutedEventArgs的時候,通過這種方式就很難獲取到了,這個時候我們就需要自己去擴展一個InvokeCommandAction了,這個時候我們應該怎么做呢?整個過程分成三步:
2.1 定義自己的CommandParameter
public class ExCommandParameter
{
/// <summary>
/// 事件觸發(fā)源
/// </summary>
public DependencyObject Sender { get; set; }
/// <summary>
/// 事件參數(shù)
/// </summary>
public EventArgs EventArgs { get; set; }
/// <summary>
/// 額外參數(shù)
/// </summary>
public object Parameter { get; set; }
}
這個對象除了封裝我們常規(guī)的參數(shù)外還封裝了我們需要的EventArgs屬性,有了這個我們就能將當前的事件的EventArgs傳遞進來了。
2.2 重寫自己的InvokeCommandAction
public class ExInvokeCommandAction : TriggerAction<DependencyObject>
{
private string commandName;
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
/// <summary>
/// 獲得或設置此操作應調用的命令的名稱。
/// </summary>
/// <value>此操作應調用的命令的名稱。</value>
/// <remarks>如果設置了此屬性和 Command 屬性,則此屬性將被后者所取代。</remarks>
public string CommandName
{
get
{
base.ReadPreamble();
return this.commandName;
}
set
{
if (this.CommandName != value)
{
base.WritePreamble();
this.commandName = value;
base.WritePostscript();
}
}
}
/// <summary>
/// 獲取或設置此操作應調用的命令。這是依賴屬性。
/// </summary>
/// <value>要執(zhí)行的命令。</value>
/// <remarks>如果設置了此屬性和 CommandName 屬性,則此屬性將優(yōu)先于后者。</remarks>
public ICommand Command
{
get
{
return (ICommand)base.GetValue(ExInvokeCommandAction.CommandProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandProperty, value);
}
}
/// <summary>
/// 獲得或設置命令參數(shù)。這是依賴屬性。
/// </summary>
/// <value>命令參數(shù)。</value>
/// <remarks>這是傳遞給 ICommand.CanExecute 和 ICommand.Execute 的值。</remarks>
public object CommandParameter
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameterProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameterProperty, value);
}
}
/// <summary>
/// 調用操作。
/// </summary>
/// <param name="parameter">操作的參數(shù)。如果操作不需要參數(shù),則可以將參數(shù)設置為空引用。</param>
protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
/*
* ★★★★★★★★★★★★★★★★★★★★★★★★
* 注意這里添加了事件觸發(fā)源和事件參數(shù)
* ★★★★★★★★★★★★★★★★★★★★★★★★
*/
ExCommandParameter exParameter = new ExCommandParameter
{
Sender = base.AssociatedObject,
Parameter = GetValue(CommandParameterProperty),
EventArgs = parameter as EventArgs
};
if (command != null && command.CanExecute(exParameter))
{
/*
* ★★★★★★★★★★★★★★★★★★★★★★★★
* 注意將擴展的參數(shù)傳遞到Execute方法中
* ★★★★★★★★★★★★★★★★★★★★★★★★
*/
command.Execute(exParameter);
}
}
}
private ICommand ResolveCommand()
{
ICommand result = null;
if (this.Command != null)
{
result = this.Command;
}
else
{
if (base.AssociatedObject != null)
{
Type type = base.AssociatedObject.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
PropertyInfo[] array = properties;
for (int i = 0; i < array.Length; i++)
{
PropertyInfo propertyInfo = array[i];
if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType) && string.Equals(propertyInfo.Name, this.CommandName, StringComparison.Ordinal))
{
result = (ICommand)propertyInfo.GetValue(base.AssociatedObject, null);
}
}
}
}
return result;
}
}
這個里面的重點是要重寫基類中的Invoke方法,將當前命令通過反射的方式來獲取到,然后在執(zhí)行command.Execute方法的時候將我們自定義的ExCommandParameter傳遞進去,這樣我們就能夠在最終綁定的命令中獲取到特定的EventArgs對象了。
2.3 在代碼中應用自定義InvokeCommandAction
<ListBox x:Name="lb_selecthistorymembers"
SnapsToDevicePixels="true"
ItemsSource="{Binding DataContext.SpecificHistoryMembers,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=my:AnnouncementApp},Mode=TwoWay}"
HorizontalAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Background="#fff"
BorderThickness="1">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<interactive:ExInvokeCommandAction Command="{Binding DataContext.OnSelectHistoryMembersListBoxSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=my:AnnouncementApp},Mode=TwoWay}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}">
</interactive:ExInvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
注意這里需要首先引入自定義的interactive的命名空間,這個在使用的時候需要注意,另外在最終的Command訂閱中EventArgs根據(jù)不同的事件有不同的表現(xiàn)形式,比如Loaded事件,那么最終獲取到的EventArgs就是RoutedEventArgs對象,如果是TableControl的SelectionChanged事件,那么最終獲取到的就是SelectionChangedEventArgs對象,這個在使用的時候需要加以區(qū)分。
3 使用當前程序集增加Behavior擴展
System.Windows.Interactivity.dll中一個重要的擴展就是對Behavior的擴展,這個Behavior到底該怎么用呢?我們來看下面的一個例子,我們需要給一個TextBlock和Button增加一個統(tǒng)一的DropShadowEffect,我們先來看看最終的效果,然后再就具體的代碼進行分析。

代碼分析
1 增加一個EffectBehavior
public class EffectBehavior : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
AssociatedObject.MouseLeave += AssociatedObject_MouseLeave;
}
private void AssociatedObject_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
var element = sender as FrameworkElement;
element.Effect = new DropShadowEffect() { Color = Colors.Transparent, ShadowDepth = 2 }; ;
}
private void AssociatedObject_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
var element = sender as FrameworkElement;
element.Effect = new DropShadowEffect() { Color = Colors.Red, ShadowDepth = 2 };
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.MouseEnter -= AssociatedObject_MouseEnter;
AssociatedObject.MouseLeave -= AssociatedObject_MouseLeave;
}
}
這里我們繼承自System.Windows.Interactivity中的Behavior<T>這個泛型類,這里我們的泛型參數(shù)使用FrameworkElement,因為大部分的控件都是繼承自這個對象,我們方便為其統(tǒng)一添加效果,在集成這個基類后我們需要重寫基類的OnAttached和OnDetaching方法,這個里面AssociatedObject就是我們具體添加Effect的元素,在我們的示例中這個分別是TextBlock和Button對象。
2 在具體的控件中添加此效果
<Window x:Class="WpfBehavior.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfBehavior" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Text="測試文本" Margin="2" Height="30"> <i:Interaction.Behaviors> <local:EffectBehavior></local:EffectBehavior> </i:Interaction.Behaviors> </TextBlock> <Button Content="測試" Width="80" Height="30" Margin="2"> <i:Interaction.Behaviors> <local:EffectBehavior></local:EffectBehavior> </i:Interaction.Behaviors> </Button> </StackPanel> </Grid> </Window>
以上就是c# WPF中System.Windows.Interactivity的使用的詳細內容,更多關于WPF中System.Windows.Interactivity的資料請關注腳本之家其它相關文章!
- 詳解Android封裝一個全局的BaseActivity
- 詳解Android Activity的啟動流程
- Android利用startActivityForResult返回數(shù)據(jù)到前一個Activity
- Android非異常情況下的Activity生命周期分析
- Android實現(xiàn)左滑退出Activity的完美封裝
- Android Activity的4種啟動模式圖文介紹
- Android IPC機制ACtivity綁定Service通信代碼實例
- 通過實例解析android Activity啟動過程
- Android用tabhost實現(xiàn) 界面切換,每個界面為一個獨立的activity操作
- springboot2.2 集成 activity6實現(xiàn)請假流程(示例詳解)
- 詳解Android Activity中的幾種監(jiān)聽器和實現(xiàn)方式
相關文章
C#應用XML作為數(shù)據(jù)庫的快速開發(fā)框架實現(xiàn)方法
這篇文章主要介紹了C#應用XML作為數(shù)據(jù)庫的快速開發(fā)框架實現(xiàn)方法,詳細介紹了將XML作為數(shù)據(jù)庫的C#桌面應用開發(fā)技巧,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
C#如何實現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù)
這篇文章主要介紹了C#如何實現(xiàn)dataGridView動態(tài)綁定數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
C#實現(xiàn)從多列的DataTable里取需要的幾列
這篇文章主要介紹了C#實現(xiàn)從多列的DataTable里取需要的幾列,涉及C#針對DataTable操作的相關技巧,需要的朋友可以參考下2016-03-03

