Search code examples
c#wpfnugetnuget-packageblend

How to reference System.Windows.Interactivity.dll from a nuget package in ResourceDictionary


I was trying to replace Blend with nuget package Microsoft.Xaml.Behaviors.Wpf so to continue using System.Windows.Interactivity.dll.

Here is the solution I tried. How to add System.Windows.Interactivity to project?

To save you a click, this is the main steps to migrate:

  1. Remove reference to “Microsoft.Expression.Interactions” and “System.Windows.Interactivity”
  2. Install the Microsoft.Xaml.Behaviors.Wpf NuGet package. XAML files
  3. 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
  4. C# files replace the usings in c# files “Microsoft.Xaml.Interactivity” and “Microsoft.Xaml.Interactions” with “Microsoft.Xaml.Behaviors”

after removing reference to “Microsoft.Expression.Interactions” and “System.Windows.Interactivity”, The following code report a error and I don't know how to resolve it. Please advise, thanks.

   <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"  
                    >

Severity Code Description Project File Line Suppression State 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


I update the code as this:

<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"  
                    >

There will be another error:

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

Severity Code Description Project File Line Suppression State 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

Severity Code Description Project File Line Suppression State 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


Solution

  • When using the Microsoft.Xaml.Behaviors.Wpf package, you should map both i: and ei: prefixes to the same XAML namespace:

    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:ei="http://schemas.microsoft.com/xaml/behaviors"
    

    Or simply use a single namespace declaration:

    <Window x:Class="WpfApp1.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:i="http://schemas.microsoft.com/xaml/behaviors"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <StackPanel>
            <Rectangle Fill="Yellow" Stroke="Black" Width="100" Height="100">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseEnter" >
                        <!-- Execute a method called 'SomeMethod' defined in the view model -->
                        <i:CallMethodAction TargetObject="{Binding}" MethodName="SomeMethod"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Rectangle>
        </StackPanel>
    </Window>