【问题标题】:Prism InvokedCommandAction is not supported in Windows Presentatioin Framework 4 projectsWindows Presentatioin Framework 4 项目不支持 Prism InvokedCommandAction
【发布时间】:2020-07-22 10:26:58
【问题描述】:

我想通过调用命令操作来使用 Prism EventTrigger,但在更高版本的 Visual Studio 中出现错误,尤其是 .Net Framework 4.0 及更高版本。 prism 库中进行了哪些更新来解决此问题?我无法使用 InvokeCommandAction 调用已绑定到 ViewModel 的命令。下面是代码sn-p和报错信息截图。

<igwpf:OutlookBarGroup  x:Class="PrismOutlook.Modules.Mail.Menus.MailGroup"
             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:local="clr-namespace:PrismOutlook.Modules.Mail.Menus"
             xmlns:ig="http://schemas.infragistics.com/xaml"
             xmlns:igwpf="http://schemas.infragistics.com/xaml/wpf"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             Header="Mail">
    <Grid>
        <ig:XamDataTree ItemsSource="{Binding Items}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="ActivaeNodeChanged">
                    <prism:InvokeCommandAction Command="{Binding SelectedCommand}" TriggerParameterPath="NewActiveTreeNode.Data" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
                <ig:XamDataTree.GlobalNodeLayouts>
                <ig:NodeLayout Key="GlobalLayout" TargetTypeName="NavigationItem" DisplayMemberPath="Caption"/>
            </ig:XamDataTree.GlobalNodeLayouts>
        </ig:XamDataTree>
    </Grid>
</igwpf:OutlookBarGroup>

她是我在解决方案输出中看到的错误消息。

我是 Prism 的新手,但对 WPF 和 MVVM 框架有很好的经验。实际上,我正在关注 Brian Lagunas 的 Youtube 教程,我意识到这是一个旧教程,并且可能对 Prism 源代码和构建进行了一些修改,存在兼容性问题。我用的是Visual Studio 2019,项目配置是.NetFramework 4.7.2,当前安装的prism版本是7.2.0.1422。

提前感谢您为解决此问题提供的帮助。

【问题讨论】:

  • 能否请您显示您的代码、错误以及您使用的 Prism 版本?

标签: c# wpf mvvm prism


【解决方案1】:

Xaml 行为现在是一个 nuget 包。

根据: https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/

迁移步骤:

删除对“Microsoft.Expression.Interactions”和“System.Windows.Interactivity”的引用 安装“Microsoft.Xaml.Behaviors.Wpf”NuGet 包。 XAML 文件 – 将 xmlns 命名空间“http://schemas.microsoft.com/expression/2010/interactivity”>和“http://schemas.microsoft.com/expression/2010/interactions”替换为“http://schemas” .microsoft.com/xaml/behaviors“ C# 文件 - 将 c# 文件“Microsoft.Xaml.Interactivity”和“Microsoft.Xaml.Interactions”中的使用替换为“Microsoft.Xaml.Behaviors”

您的事件触发器还必须是尚未在其他地方处理的路由事件。

https://docs.microsoft.com/en-us/dotnet/api/system.windows.eventtrigger?view=netcore-3.1

【讨论】:

    【解决方案2】:

    这似乎不是 Prism 的问题,而是 Blend SDK 库的问题。您的代码应该可以正常工作,因为 InvokeCommandAction 派生自 System.Windows.Interactivity 中的 TriggerAction&lt;T&gt; 并且版本 7.2.0.1422 不使用它的 版本。

    过去也有类似的问题,不是Prism的实现引起的,估计是SDK库没有正确注册。有关 Prism 5.0 和 6.0 的参考和解决方案,请参阅此 issue。在这种情况下似乎起作用的是手动将新版本的库注册到全局程序集缓存。在这个问题上还有一个related post

    gacutil -i "C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4.5\Libraries\System.Windows.Interactivity.dll"
    

    相关文章中提到的另一个选项是使用Microsoft.Xaml.Behaviors.Wpf NuGet 包而不是System.Windows.Interactivity。虽然我建议您尽可能使用它,但与 Prism 的InvokeCommandAction 一起使用时会触发另一个警告,因此一旦新版本的 Prism 将其类型基于此新包,您可能应该升级到此解决方案。

    无效类型:预期类型为“TriggerAction”,实际类型为“InvokeCommandAction”

    【讨论】:

      猜你喜欢
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      相关资源
      最近更新 更多