简体   繁体   English

如何在项目中添加System.Windows.Interactivity?

[英]How to add System.Windows.Interactivity to project?

My project missing System.Windows.Interactivity .我的项目缺少System.Windows.Interactivity Google says that I have to install Expression Blend, but on my other computer I have this library and I don't have Expression Blend installed. Google 说我必须安装 Expression Blend,但在我的另一台计算机上有这个库,但我没有安装 Expression Blend。 So there should be another way to obtain System.Windows.Interactivity ?那么应该有另一种方法来获取System.Windows.Interactivity吗? What should I do?我应该怎么办? (right now i don't have another computer so I can not just copy this library:) (现在我没有另一台电脑所以我不能只复制这个库:)

Although this issue is quite old, i think this is relevant news / the most recent answer: Microsoft open-sourced XAML Behaviours and posted a blog post how to update to this version: https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/虽然这个问题已经很老了,但我认为这是相关新闻/最新答案:Microsoft 开源 XAML 行为并发布了一篇博客文章如何更新到此版本: https : //devblogs.microsoft.com/dotnet/open -source-xaml-behaviors-for-wpf/

To save you a click, this is the main steps to migrate:为了节省您的点击,这是迁移的主要步骤:

  1. Remove reference to “Microsoft.Expression.Interactions” and “System.Windows.Interactivity”删除对“Microsoft.Expression.Interactions”和“System.Windows.Interactivity”的引用
  2. Install the Microsoft.Xaml.Behaviors.Wpf NuGet package.安装Microsoft.Xaml.Behaviors.Wpf NuGet 包。
  3. XAML files – replace the xmlns namespaces http://schemas.microsoft.com/expression/2010/interactivity and http://schemas.microsoft.com/expression/2010/interactions with http://schemas.microsoft.com/xaml/behaviors XAML 文件 – 将 xmlns 命名空间http://schemas.microsoft.com/expression/2010/interactivityhttp://schemas.microsoft.com/expression/2010/interactions替换为http://schemas.microsoft.com/xaml /行为
  4. C# files – replace the usings in c# files “Microsoft.Xaml.Interactivity” and “Microsoft.Xaml.Interactions” with “Microsoft.Xaml.Behaviors” C# 文件 – 用“Microsoft.Xaml.Behaviors”替换 c# 文件“Microsoft.Xaml.Interactivity”和“Microsoft.Xaml.Interactions”中的使用

The official package for behaviors is Microsoft.Xaml.Behaviors.Wpf .行为的官方包是Microsoft.Xaml.Behaviors.Wpf

It used to be in the Blend SDK (deprecated).它曾经在Blend SDK 中(已弃用)。
See Jan's answer for more details if you need to migrate.如果您需要迁移,请参阅Jan 的回答以了解更多详细信息。

There is a new NuGet package that contains the System.Windows.Interactivity.dll that is compatible with:有一个新的 NuGet 包,其中包含与以下兼容的 System.Windows.Interactivity.dll:

  • WPF 4.0, 4.5 WPF 4.0、4.5
  • Silverligt 4.0, 5.0银光 4.0、5.0
  • Windows Phone 7.1, 8.0视窗电话 7.1、8.0
  • Windows Store 8, 8.1 Windows 应用商店 8、8.1

To install Expression.Blend.Sdk, run the following command in the Package Manager Console要安装 Expression.Blend.Sdk,请在包管理器控制台中运行以下命令

PM> Install-Package Expression.Blend.Sdk

http://www.nuget.org/packages/Expression.Blend.Sdk/ http://www.nuget.org/packages/Expression.Blend.Sdk/

The easiest way might be to get it from NuGet:最简单的方法可能是从 NuGet 获取它:

http://www.nuget.org/packages/System.Windows.Interactivity.WPF/ http://www.nuget.org/packages/System.Windows.Interactivity.WPF/

With Blend for Visual Studio , which is included in Visual Studio starting with version 2013, you can find the DLL in the following folder:使用Blend for Visual Studio (从 2013 版开始包含在 Visual Studio 中),您可以在以下文件夹中找到 DLL:

C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.5\Libraries

You will have to add the reference to the System.Windows.Interactivity.dll yourself though, unless you use Blend for Visual Studio with an existing project to add functionality that makes use of the Interactivity namespace.不过,您必须自己添加对System.Windows.Interactivity.dll的引用,除非您将 Blend for Visual Studio 与现有项目一起使用以添加使用 Interactivity 命名空间的功能。 In that case, Blend will add the reference automatically.在这种情况下,Blend 将自动添加引用。

Sometimes, when you add a new library, in introduces a clashing version of System.Windows.Interactivity.dll .有时,当您添加新库时,会引入System.Windows.Interactivity.dll的冲突版本。

For example, the NuGet package MVVM light might require v4.2 of System.Windows.Interactivity.dll , but the NuGet package Rx-XAML might require v4.5 of System.Windows.Interactivity.dll .例如,NuGet 包MVVM light可能需要System.Windows.Interactivity.dll v4.2,但 NuGet 包Rx-XAML可能需要System.Windows.Interactivity.dll v4.5。 This will prevent the the project from working, because no matter which version of System.Windows.Interactivity.dll you include, one of the libraries will refuse to compile.这将阻止项目工作,因为无论您包含哪个版本的System.Windows.Interactivity.dll ,其中一个库都将拒绝编译。

To fix, add an Assembly Binding Redirect by editing your app.config to look something like this:要修复,请通过编辑app.config来添加程序集绑定重定向,如下所示:

<?xml version="1.0"?>
<configuration>
<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Windows.Interactivity"
                        publicKeyToken="31bf3856ad364e35"
                        culture="neutral"/>
      <bindingRedirect oldVersion="4.0.0.0"
                       newVersion="4.5.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<appSettings>
  <add key="TestKey" value="true"/>
</appSettings>

Don't worry about changing the PublicKeyToken , that's constant across all versions, as it depends on the name of the .dll, not the version.不要担心更改PublicKeyToken ,它在所有版本中都是不变的,因为它取决于 .dll 的名称,而不是版本。

Ensure that you match the newVersion in your appConfig to the actual version that you end up pointing at:确保您将newVersion中的appConfig与您最终指向的实际版本相匹配:

在此处输入图片说明

Alternative solution is to modify your current Visual Studio installation in the Visual Studio Installer替代解决方案是在 Visual Studio 安装程序中修改您当前的 Visual Studio 安装

Win + R %ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vs_installer.exe Win + R %ProgramFiles(x86)%\\Microsoft Visual Studio\\Installer\\vs_installer.exe

adding the Blend for Visual Studio SDK for .NET 'Individual component' under 'SDKs, libraries, and frameworks':在“SDK、库和框架”下添加Blend for Visual Studio SDK for .NET “Individual component”:

在此处输入图片说明 after adding this component System.Windows.Interactivity should appear in its regular location Add Reference/Assemblies/Extensions .添加此组件后System.Windows.Interactivity应出现在其常规位置Add Reference/Assemblies/Extensions


It appears this would only work for VS2017 or earlier.看来这仅适用于 VS2017 或更早版本。 For later versions, please refer to other answers.对于以后的版本,请参考其他答案。

I have had the exact same problem with a solution, that System.Windows.Interactivity was required for one of the project in Visual Studio 2019, and I tried to install Blend for Visual Studio SDK for .NET from Visual Studio 2019 Individual components, but it did not exist in it.我在解决方案中遇到了完全相同的问题,即 Visual Studio 2019 中的一个项目需要System.Windows.Interactivity ,我尝试从 Visual Studio 2019 单个组件安装Blend for Visual Studio SDK for .NET ,但是它不存在于其中。

The consequence of that, I was not able to build the project in my solution with repetitive of following similar error on different XAML parts of the project:结果,我无法在我的解决方案中构建项目,并且在项目的不同 XAML 部分重复遵循类似的错误:

The tag 'Interaction.Behaviors' does not exist in XML namespace 'clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity'. XML 命名空间“clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity”中不存在标记“Interaction.Behaviors”。

包含所有错误的图像 The above mentioned errors snapshot example上面提到的错误快照示例

The solution , the way I solved it, is by installing Microsoft Expression Blend Software Development Kit (SDK) for .NET 4 from Microsoft.解决方案,我解决它的方式,是通过从 Microsoft 安装 适用于 .NET 4 的 Microsoft Expression Blend 软件开发工具包 (SDK)

Thanks to my colleague @felza, mentioned that System.Windows.Interactivity requires this sdk, that is suppose to be located in this folder:感谢我的同事@felza,提到System.Windows.Interactivity需要这个 sdk,假设位于这个文件夹中:

C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0

In my case it was not installed.就我而言,它没有安装。 I have had this folder C:\\Program Files (x86)\\Microsoft SDKs with out Expression\\Blend\\.NETFramework\\v4.0 folder inside it.我有这个文件夹C:\\Program Files (x86)\\Microsoft SDKs没有Expression\\Blend\\.NETFramework\\v4.0文件夹。

After installing it, all errors disappeared.安装后,所有错误都消失了。

它在 MVVM Light 中,从MVVM Light 下载页面获取

I got it via the Prism.WPF NuGet-Package.我是通过 Prism.WPF NuGet-Package 获得的。 (it includes Windows.System.Interactivity) (它包括 Windows.System.Interactivity)

If you are working with MVVM Light you have to use the System.Windows.Interactivity Version 4.0 (the NuGet .dll wont work) that you can find under :如果您正在使用 MVVM Light,则必须使用System.Windows.Interactivity Version 4.0 (NuGet .dll 不起作用),您可以在以下位置找到:

PathToProjectFolder\\Software\\packages\\MvvmLightLibs.5.4.1.1\\lib\\net45\\System.Windows.Interactivity.dll

Just add this .dll manually as Reference and it should be fine.只需手动添加这个 .dll 作为参考,它应该没问题。

I was getting the following error using Visual Studio 2019:我在使用 Visual Studio 2019 时遇到以下错误:

System.Windows.Markup.XamlParseException: 'Could not load file or assembly 'System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. System.Windows.Markup.XamlParseException: '无法加载文件或程序集'System.Windows.Interactivity,Version=4.5.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35'或其依赖项之一。 The located assembly's manifest definition does not match the assembly reference.定位的程序集的清单定义与程序集引用不匹配。 (Exception from HRESULT: 0x80131040)' (来自 HRESULT 的异常:0x80131040)'

Our project is using packages.config and had NuGet references to MVVMLightLibs 5.4.1.1 and System.Windows.Interactivity.WPF 2.0.20525 .我们的项目正在使用 packages.config 并且 NuGet 引用了MVVMLightLibs 5.4.1.1System.Windows.Interactivity.WPF 2.0.20525

Guided by some of the other answers here, I realised the problem was a conflict between the version of the DLL that MVVMLight was using and the one our project was using.在此处的其他一些答案的指导下,我意识到问题在于 MVVMLight 使用的 DLL 版本与我们的项目使用的 DLL 版本之间存在冲突。 The solution was:解决方案是:

  • Remove the NuGet reference to System.Windows.Interactivity .删除 NuGet 对System.Windows.Interactivity引用。
  • Added a Reference to packages\\MvvmLightLibs.5.4.1.1\\lib\\net45\\System.Windows.Interactivity.dll .添加了对packages\\MvvmLightLibs.5.4.1.1\\lib\\net45\\System.Windows.Interactivity.dll的引用。
  • Add (in our case, update) a Binding Redirect to app.config:添加(在我们的例子中,更新)一个绑定重定向到 app.config:
<dependentAssembly>
    <assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
</dependentAssembly>

Use the following two NuGet packages使用下面两个NuGet包

1.Microsoft.Xaml.Behaviors.wpf 1.微软.Xaml.行为.wpf

2.Expression.Blend.Sdk.WPF 2.表达.混合.Sdk.WPF

Moreover while installing VS2022 see option to install blend sdk. click on the checkbox while installing.If you have not found that check box then tick the UWP and then you can see that option while installing VS 2022.此外,在安装 VS2022 时,请参阅安装 blend sdk 的选项。在安装时单击该复选框。如果您没有找到该复选框,则勾选 UWP,然后您可以在安装 VS 2022 时看到该选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM