问题描述
我正在使用 MVVM Light 处理 WPF 项目并尝试使用 EventToCommand
,但我收到一条错误消息:
“EventToCommand”类型的值不能添加到“TriggerActionCollection”类型的集合或字典中。”
我使用的是 .NET Framework 4.7.2 和 NuGet 库 Microsoft.Xaml.Behaviors.Wpf。
我看到大多数地方都在谈论 http://schemas.microsoft.com/expression/2010/interactivity
,
但据我所知,这现在已被弃用,应改用 http://schemas.microsoft.com/xaml/behaviors
。
在 mvvmlight.net 的更新日志中,我发现了这个:
EventToCommand - BL0004.005,程序集“galaSoft.MvvmLight.Extras.WPF4”中的类型“EventToCommand”是使用旧版本的 Blend SDK 构建的,在 Windows Presentation Framework 4 项目中不受支持。
我已尝试更改项目目标框架版本,但似乎没有任何区别。
任何帮助将不胜感激。
<UserControl x:Class="DialogueTree.View.DialogueControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:DialogueTree.View"
xmlns:command="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="200"
MinHeight="200" MinWidth="200">
<TextBox Text="{Binding Headline}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<command:EventToCommand Command="{Binding Source={StaticResource Locator},Path=DialogueControlVM.EndEditHeadline}"
CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
解决方法
来自 MvvmLight 的 EventToCommand
操作基于旧版 Blend SDK 程序集。
- XML 命名空间
http://schemas.microsoft.com/expression/2010/interactivity
- CLR 命名空间
System.Windows.Interactivity
这些实际上已弃用并被您使用的开源 XAML 行为取代。问题是这些程序集不兼容,因此您不能将 MvvmLight 提供的操作与 XAML 行为一起使用,反之亦然。我猜 MvvmLight 将来也会更新以使用这些行为。 Here is an open issue 关于这个主题。
但是,您不需要 EventToCommand
,因为 XAML 行为包已经包含执行相同操作的命令:InvokeCommandAction
。不过,属性名称可能不同。
<b:EventTrigger EventName="LostFocus">
<b:InvokeCommandAction Command="{Binding Source={StaticResource Locator},Path=DialogueControlVM.EndEditHeadline}"
CommandParameter="{Binding}"/>
</b:EventTrigger>