wpf 为什么 System.Windows.Interactivity 不想工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24848184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why System.Windows.Interactivity doesn't want to work?
提问by Bastien Vandamme
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. Now when I try to add an interactivity tag in mt XAML file I get an complete error on this namespace.
首先,我收到未定义的 CLR 命名空间错误,但在构建后已修复。现在,当我尝试在 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,但从未打开或使用过它。
回答by Moti Azu
This is the correct namespace for interactivity within XAML:
这是 XAML 中用于交互的正确命名空间:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
回答by Rohit Vats
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"
回答by Contango
First, add the MVVM LightNuGet package, which will add a reference to System.Windows.Interactivity.dll
首先,添加MVVM LightNuGet 包,该包将添加对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.configto 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 newVersionin your appConfigto the actual version that you end up pointing at:
确保您将newVersion中的appConfig与您最终指向的实际版本相匹配:
回答by M.Pogorzelski
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, e.g. CallMethodAction.
Blend will automatically add references to System.Windows.Interactivityand 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.
还有一个选项可以使用 Blend for Visual Studio 本身在需要的地方添加所有必要的引用。您只需在 Blend 中打开您的解决方案,然后导航到您想要添加 Blend 行为的视图文件。打开视图后,您必须从解决方案资源管理器切换到资产选项卡,选择行为并双击想要的操作,例如 CallMethodAction。Blend 将自动添加对System.Windows.Interactivity和Microsoft.Expression.Interactions、XML 命名空间的引用并生成 XAML 代码。您必须在 Blend 中保存更改的文件,切换到 Visual Studio 并重新加载解决方案以应用更改。


