简体   繁体   中英

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

I just added this line to my XAML file:

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

First I get an Undefined CLR namespace error but after a build it was fixed. Now when I try to add an interactivity tag in mt XAML file I get an complete error on this namespace.

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.

这是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

However, sometimes, when you add a new NuGet package, in might introduce a clashing version of 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:

<?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.

Ensure that you match the newVersion in your appConfig to the actual version that you end up pointing at:

在此处输入图片说明

There is also an option to use Blend for Visual Studio itself to add all necessary references where needed. You simply open up your solution in Blend, then you navigate to the view file you want Blend behavior to add to. After opening the view, you have to switch from Solution Explorer to Assets tab, select Behaviors and double-click wanted action, eg CallMethodAction. Blend will automatically add references to System.Windows.Interactivity and Microsoft.Expression.Interactions , XML namespaces and also generate XAML code. You have to save changed file in Blend, switch to Visual Studio and reload solution to apply changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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