6

I have a WPF assembly that i use in an interop scenario from native code using the LoadFrom loading context like this:

AppDomain.CreateInstanceFrom("c:\mydlls\mywpfstuff.dll", "myclass")

Note that mydlls is not the same folder as where the executable is located. This works fine for regular non-ui .NET dlls that i also load, but when I try to do this I get an error. I attached the AppDomain.CurrentDomain.AssemblyResolve event handler and a get a an event where it fails to load. The Name in the ResolveEventArgs is "mywpfstuff.resources" and the RequestingAssembly is empty. I have no file named "mywpfstuff.resources" and could not figure out how to do this assembly resolving myself.

The code line that triggers the error is the InitializeComponent(); call in my main user controls constructor.

It seems to me that the internal XAML (BAML?) mechanisms tries to load some resources, but uses that standard Load context instead of the LoadFrom context.

Is there any way around this problem, preferably by getting WPF to use the LoadFrom context or if that is not possible how to do the assembly resolving manually?

Bragosaurus
  • 116
  • 8
  • Do you have a file with name `Resources.resx` in `mywpfstuff` project (probably in `Properties` folder)? If so what `Build Action` it has? – SHSE Apr 07 '13 at 12:06
  • I'm not sure that matters much - you'll see many dll-s tried to be loaded when using AssemblyResolve (in my experience). Do you have anything else to add about your project - what's 'myclass' exactly? – NSGaga-mostly-inactive Apr 07 '13 at 19:03
  • Have you tried to return mywpfstuff assembly from your event handler when the mywpfstuff.resources is requested? – mgronber Apr 09 '13 at 14:33

2 Answers2

0

I had similar problem in the past, due to localization issues and missing resx.

If the XAML uses resources from that assembly, double-check that the resources for the culture of the UI are actually available in the proper subfolder of c:\mydlls .

Giacomo Tesio
  • 7,144
  • 3
  • 31
  • 48
0

I had a similar scenario when I created this Unused References – VS2010 Add-in – top to bottom.

The problem is that the resources already loaded, and you cannot reload another resources.

Hope this helps...

I created a Start method to be accessed:

public static void Start()
{
    if (Application.Current == null)
    {
        // create the Application object
        App a = new App();
        var l = a.Resources["Locator"] as Locator;
        // do something with l
        a.Run();
    }
    else
    {
        var locator = new Locator();
        // do something with l
        Application.Current.Resources.Remove("Locator");
        Application.Current.Resources.Add("Locator", locator);
        MainWindow main = new MainWindow();
        main.Show();
    }
}
Ofir
  • 2,174
  • 1
  • 16
  • 15