microsoft / XamlBehaviorsWpf

Home for WPF XAML Behaviors on GitHub.
MIT License
840 stars 138 forks source link

Could not load file or assembly Microsoft.Xaml.Behaviors #86

Closed quasarea closed 1 year ago

quasarea commented 3 years ago

Describe the bug Using behaviors in my xaml brings compilation error message.

To Reproduce

<Window x:Class="Aveva.SplashScreen.Sample.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        >
    <b:Interaction.Triggers>
        <b:EventTrigger EventName="MouseDown" >
            <b:InvokeCommandAction 
                Command="{Binding MouseDownCommand}" 
                CommandParameter="{Binding Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window }}"  />
        </b:EventTrigger>
    </b:Interaction.Triggers>

Error

System.Windows.Markup.XamlParseException: 'Could not load file or assembly 'Microsoft.Xaml.Behaviors, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

Expected behavior To work out of the box, without having to reference library in code behind. Because it works fine if I reference anything:

        public MainWindow()
        {
            InitializeComponent();

            var _ = new Microsoft.Xaml.Behaviors.DefaultTriggerAttribute(typeof(Trigger), typeof(Microsoft.Xaml.Behaviors.TriggerBase), null);
        }

Desktop (please complete the following information):

quasarea commented 3 years ago

Spotted at https://github.com/zspitz/ANTLR4ParseTreeVisualizer/issues/35

brianlagunas commented 3 years ago

Please provide a sample that reproduces the issue or this issue will be closed.

quasarea commented 3 years ago

Had to be some mess at my side, sorry, could not replicate.

Jebarson commented 3 years ago

This a repro for me. It happens when the Page is in a class library. The easiest workaround is to name behavior command in the xaml.

<Behaviors:InvokeCommandAction x:Name="SelectionCommand" Command="{Binding SelectedItemsChangedCommand}"

keparanj commented 3 years ago

Happens for me as well (however, I get a runtime exception similar to https://github.com/zspitz/ANTLR4ParseTreeVisualizer/issues/35#issue-505087118) when the WPF project is a class library. Interestingly when I tried to reproduce it in a fresh sample project, I wasn't able to. Perhaps there is something else that is interfering with it. May dig deeper if I get more time

Like @quasarea mentioned, a quick fix is to simply reference something from within the Microsoft.Xaml.Behaviors package before the call to the Window's InitializeComponent()

LouisCPro commented 2 years ago

Hi,

Same problem with version 1.1.37 of the Microsoft.Xaml.Behaviors Library !

Just added this in the CTOR of the control where I use it:

public MyControl() { var trig = new Microsoft.Xaml.Behaviors.EventTrigger(); trig.SourceName = "foo"; // InitializeComponent(); }

And no error after that !

jerrud commented 2 years ago

I have the OPs same problem using version 1.1.39. The view is simply a Window in a folder in a class library. The library is a plug-in for another application. Creating a new WPF application and loading in that app as a plugin to the host app does not have this exception.

I was able to use OPs work around and view now works:

        public MainWindow()
        {
            InitializeComponent();

            var _ = new Microsoft.Xaml.Behaviors.DefaultTriggerAttribute(typeof(Trigger), typeof(Microsoft.Xaml.Behaviors.TriggerBase), null);
        }
weitzhandler commented 1 year ago

I have the same issue when trying to add a ChangePropertyAction in page located in a referenced class library. Adding a x:Name to the action solved the problem as @Jebarson recommended.

Sh3rlck commented 1 year ago

Currently having the same issue. Using it on a class library. var _ = new Microsoft.Xaml.Behaviors.DefaultTriggerAttribute(typeof(Trigger), typeof(Microsoft.Xaml.Behaviors.TriggerBase), null); This does the trick for me as well, wondering is there has been a fix for this or if there is a cleaner way. I will most likely have multiple wpf windows that will requiere access to triggers and I dont want to add this line of code to every single one.

Any help is appreciated.

Boorda commented 6 months ago

Same issue, I installed the NuGet pkg and tried to use this interaction logic:

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

<Button Content="Button 1">
    <b:Interaction.Triggers>
        <b:EventTrigger EventName="Click">
            <b:InvokeCommandAction Command="{Binding Button1Command}" PassEventArgsToCommand="True" />
        </b:EventTrigger>
    </b:Interaction.Triggers>
</Button>

...
wegewitzSte commented 1 month ago

Same issue, For a net48 WPF project, Doesnt occur on class libaries net48 though.