如何在ResourceDictionary中通过NuGet引用System.Windows.Interactivity.dll并解决迁移错误
我尝试用NuGet包Microsoft.Xaml.Behaviors.Wpf替代Blend的System.Windows.Interactivity.dll,按以下步骤迁移:
- 移除对
Microsoft.Expression.Interactions和System.Windows.Interactivity的引用 - 安装
Microsoft.Xaml.Behaviors.WpfNuGet包 - 将XAML文件中的命名空间
http://schemas.microsoft.com/expression/2010/interactivity和http://schemas.microsoft.com/expression/2010/interactions替换为http://schemas.microsoft.com/xaml/behaviors - 在C#文件中将using语句从
Microsoft.Xaml.Interactivity和Microsoft.Xaml.Interactions替换为Microsoft.Xaml.Behaviors
第一次错误场景
移除旧引用后,以下XAML代码报错:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" <!--line 3--> xmlns:ei="http://schemas.microsoft.com/xaml/behaviors" >
错误信息:
Error XLS0418 Assembly 'System.Windows.Interactivity' was not found. Verify that you are not missing an assembly reference. Also, verify that your project and all referenced assemblies have been built. License C:\GitHub\Dependency\licenseAPI\License\License\Assets\Modern_Resource_Dictionary.xaml 3
修改后的错误场景
我将XAML命名空间修改为:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:ei="http://schemas.microsoft.com/xaml/behaviorss" >
随后这段行为代码出现新错误:
<i:Interaction.Behaviors> <ei:FluidMoveBehavior AppliesTo="Children" Duration="0:0:0.25"> <ei:FluidMoveBehavior.EaseX> <SineEase EasingMode="EaseIn" /> </ei:FluidMoveBehavior.EaseX> </ei:FluidMoveBehavior> </i:Interaction.Behaviors>
错误信息:
Error XDG0008 The name "FluidMoveBehavior" does not exist in the namespace "http://schemas.microsoft.com/xaml/behaviorss". HPTCCSLicense C:\GitHub\Dependency\licenseAPI\License\License\Assets\Modern_Resource_Dictionary.xaml 239
Error XLS0414 The type 'ei:FluidMoveBehavior' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. HPTCCSLicense C:\GitHub\Dependency\licenseAPI\License\License\Assets\Modern_Resource_Dictionary.xaml 239
1. 修正命名空间拼写错误
你把ei命名空间写成了http://schemas.microsoft.com/xaml/behaviorss(多了一个末尾的s),正确的命名空间是http://schemas.microsoft.com/xaml/behaviors。
2. 统一命名空间使用
Microsoft.Xaml.Behaviors.Wpf包将原System.Windows.Interactivity和Microsoft.Expression.Interactions的内容合并到了同一个命名空间下,不需要区分i和ei两个前缀,直接统一使用一个命名空间即可:
修改后的ResourceDictionary命名空间:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" >
对应的行为代码修改为:
<b:Interaction.Behaviors> <b:FluidMoveBehavior AppliesTo="Children" Duration="0:0:0.25"> <b:FluidMoveBehavior.EaseX> <SineEase EasingMode="EaseIn" /> </b:FluidMoveBehavior.EaseX> </b:FluidMoveBehavior> </b:Interaction.Behaviors>
3. 确认NuGet包安装状态
右键项目 → 管理NuGet程序包,确认Microsoft.Xaml.Behaviors.Wpf已正确安装;如果存在安装异常,尝试卸载后重新安装,确保包版本与项目的.NET框架版本兼容。
4. 清理并重建项目
修改完成后,先清理项目(菜单→项目→清理),再重建整个解决方案(菜单→生成→重建解决方案),确保修改生效。
内容的提问来源于stack exchange,提问作者Ming

