简体   繁体   English

为什么System.Windows.Interactivity不想工作?

[英]Why System.Windows.Interactivity doesn't want to work?

I just added this line to my XAML file: 我刚刚将此行添加到我的XAML文件中:

xmlns:interactivity="clr-namespace:System.Windows.Interactivity"

First I get an Undefined CLR namespace error but after a build it was fixed. 首先,我得到一个未定义的CLR名称空间错误,但是在构建之后,它已修复。 Now when I try to add an interactivity tag in mt XAML file I get an complete error on this namespace. 现在,当我尝试在mt XAML文件中添加交互性标记时,在此名称空间上出现了一个完整的错误。

Here is a sample of my code 这是我的代码示例

<Window x:Class="ColorTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:interactivity="clr-namespace:System.Windows.Interactivity"
    xmlns:ignore="http://www.ignore.com"
    mc:Ignorable="d ignore"
    Height="576"
    Width="720"
    Title="MVVM Light Application"
    DataContext="{Binding Main, Source={StaticResource Locator}}">

<Grid x:Name="LayoutRoot" Background="#FF44494D">
    <Rectangle x:Name="Color01" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Margin="10,29,0,0" Stroke="Black" VerticalAlignment="Top" Width="100">
        <!--<interactivity:Interaction.Triggers>
            <interactivity:EventTrigger EventName="MouseDown">
            <interactivity:InvokeCommandAction Command="{Binding MyCommand}"/>
        </interactivity:EventTrigger>
        </interactivity:Interaction.Triggers>-->
    </Rectangle>
  </Grid>

To help here is a part of my project file with my references 在这里提供帮助是我的项目文件的一部分,并带有我的参考资料

    <Reference Include="GalaSoft.MvvmLight.Extras.WPF45">
      <HintPath>packages\MvvmLightLibs.4.2.30.0\lib\net45\GalaSoft.MvvmLight.Extras.WPF45.dll</HintPath>
    </Reference>
    <Reference Include="GalaSoft.MvvmLight.WPF45">
      <HintPath>packages\MvvmLightLibs.4.2.30.0\lib\net45\GalaSoft.MvvmLight.WPF45.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Practices.ServiceLocation">
      <HintPath>packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>packages\MvvmLightLibs.4.2.30.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
    </Reference>
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />

To finish I would like to let you know I installed Blend but never opened it or used it. 最后,我想告诉您我安装了Blend,但从未打开或使用过它。

这是XAML中进行交互的正确名称空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

You need to provide assembly name for namespaces which resides in different assemblies. 您需要为驻留在不同程序集中的名称空间提供程序集名称。 Declare namespace like this: 这样声明名称空间:

xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

First, add the MVVM Light NuGet package, which will add a reference to System.Windows.Interactivity.dll 首先,添加MVVM Light NuGet程序包,该程序包将添加对System.Windows.Interactivity.dll的引用

However, sometimes, when you add a new NuGet package, in might introduce a clashing version of System.Windows.Interactivity.dll . 但是,有时,当您添加新的NuGet程序包时,可能会引入System.Windows.Interactivity.dll的冲突版本。

This prevents the project from working. 这会阻止项目正常工作。

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与最终指向的实际版本相匹配:

在此处输入图片说明

There is also an option to use Blend for Visual Studio itself to add all necessary references where needed. 还有一个选项可以使用Blend for Visual Studio本身在需要的地方添加所有必要的引用。 You simply open up your solution in Blend, then you navigate to the view file you want Blend behavior to add to. 您只需在Blend中打开解决方案,然后导航到您要添加Blend行为的视图文件。 After opening the view, you have to switch from Solution Explorer to Assets tab, select Behaviors and double-click wanted action, eg CallMethodAction. 打开视图后,您必须从解决方案资源管理器切换到“资产”选项卡,选择“行为”,然后双击所需的操作,例如CallMethodAction。 Blend will automatically add references to System.Windows.Interactivity and Microsoft.Expression.Interactions , XML namespaces and also generate XAML code. Blend将自动添加对System.Windows.InteractivityMicrosoft.Expression.Interactions (XML名称空间)的引用,并生成XAML代码。 You have to save changed file in Blend, switch to Visual Studio and reload solution to apply changes. 您必须将更改的文件保存在Blend中,切换到Visual Studio并重新加载解决方案以应用更改。

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

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