微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

EventToCommand 不能添加到 TriggerActionCollection

如何解决EventToCommand 不能添加到 TriggerActionCollection

我正在使用 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>

解决方法

来自 MvvmLightEventToCommand 操作基于旧版 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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。