Wednesday, July 22, 2015

Quick Fix: Could not load file or assembly when instantiating XAML component (or IoC component) in a Visual Studio Extension Project

Symptom
You're writing a Visual Studio extension that involves some XAML or IoC code that references a DLL file. When you attempt to instantiate the control, the debugger pops up with "Could not load file or assembly". You've checked the Fusion log and notice it isn't looking in the folder that contains the extension!
Fix
Add the attribute [ProvideBindingPath] to the class that represents the package (the class that implements the Package base class which is often completely empty).
Why?!
Visual Studio's Extension engine usually figures out where your reference .dll's are and binds them easily, however, when a .dll is referenced in XAML or IoC, it isn't explicit enough for Visual Studio to handle proper binding. The ProvideBindingPath attribute tells the extension engine to look in the extension folder when attempting to resolve dependencies. In my case it was a fancy options page that used XAML for its UI rather than the automatically generated UI.

No comments: