LoadComponent - Beta2 --> June CTP
- In WinFx Beta 2 I was able to do the following.
In a seperate assembly called MyPluginAssembly (that was not referenced by my application) I had a xaml file called MyResources.xaml that looked like this:<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
MyNameSpace exists within MyPluginAssembly and has a custom control called MyCustomControl.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:im="clr-namespace:MyNameSpace"
>
<ControlTemplate x:Key="MyKey">
<im:MyCustomControl Width="100"/>
</ControlTemplate>
</ResourceDictionary>
In my application I did this:
Application.LoadComponent(new Uri(string.Format(@"{0};;;component\MyResources.xaml", formatType.Assembly.FullName), UriKind.Relative));
I could then cast the result of LoadComponent to a ResourceDictionary and be on my merry way, using the resources as I saw fit.
Now with the June CTP and the same code I get an error:
"Cannot find type 'MyNameSpace.MyCustomControl' The assembly used when compiling might be different than that used when loading and the type is missing."
I know that it is the correct version and if I reference the assembly in my main application the code runs fine - this isn't acceptable however as it would mean I no longer have a plugin architecture.
Here is my take on what is happening.
Between Beta 2 and June CTP it looks like a change has happened with how references are resolved when using LoadComponent. Note that in my xaml I have not specified the assembly where the custom control should load from (nor can I because the control is in the same project as the xaml and it won't allow an assembly reference to itself). I think that previously it was looking in the assembly where the component was loaded from, and now it is looking in the application assembly and assemblies referenced by the application.
Am I onto something here or is this completely off base?
One thing I do know is that what did work on Beta 2 does not work on June.
Can someone offer a workaround for this behaviour?
Thanks for your help.
답변
- Can you try changing the AssemblyVersion attribute value to a static value instead of the default 1.0.0.*, rebuild and see if this problem can be reproduced? Seems like a known bug in June/July CTPs but I want to make sure.
모든 응답
- Any ideas?
I still haven't been able to solve this. - Can you try changing the AssemblyVersion attribute value to a static value instead of the default 1.0.0.*, rebuild and see if this problem can be reproduced? Seems like a known bug in June/July CTPs but I want to make sure.
- Yes this works. Thanks very much!
This trick is not working when I try to load different assemblies dynamically.
For examle:
1. I compile assembly with version 1.0.0.0 on the fly with Form1.xaml - and class Form1
2. I dynamically load this assembly and successfully load form by
System.Windows.
Application.LoadComponent(new Uri(string
.Format("{0};component\\Form1.xaml", aAssemblyFullName) , System.UriKind.RelativeOrAbsolute));3. I compile another assembly with version 1.0.0.2 on the fly with Form1.xaml - and class Form2
4. Then I load this assembly and got error when I use LoadComponent. ("The assembly used when compiling might be different....")
If I don't change class name to Form2 on the step 2 - load will successfull, but code will be used from old assembly (1.0.0.0). I mean xaml file will be from 1.0.0.2 and event handlers will be from 1.0.0.0.
Eugene-
You refer to step 2...but I didn't see any mention of Form2 in step2. Can you clarify?
All-
Please see my blog post about this issue: http://rrelyea.spaces.msn.com/blog/cns!167AD7A5AB58D5FE!497.entry
Thx, Rob Relyea
WPF PMFirst scenario:
1. Build assembly with strong name, version 1.0.0.0; Form1.xaml; class Form1 with EventHandlers
2. Try to load by LoadComponent -> All is fine.
3. Build another assembly with strong name, version 1.0.0.2; Form1.xaml (with the same name, but different content); class Form1 with EventHandlers (with the same name, but different content)
4. Try to load by LoadComponent: As result I will have: succeseful load with GUI layout from assembly version 1.0.0.2, but EventHandlers from assembly version 1.0.0.0
Second scenario:
1. Build assembly with strong name, version 1.0.0.0; Form1.xaml; class Form1 with EventHandlers
2. Try to load by LoadComponent -> All is fine.
3. Build another assembly with strong name, version 1.0.0.2; Form1.xaml (with the same name, but different content); class Form2 with EventHandlers
4. Try to load by LoadComponent: so I got exception
"Error in markup file 'Form1, Version=1.0.0.2, Culture=neutral, PublicKeyToken=421f993a8952c890;component/form1.xaml' : Cannot find type 'XamlExportedObjects.Form2'. The assembly used when compiling might be different than that used when loading and the type is missing."
If I try to load assembly in another AppDomain and Unload it befor loading another assembly - All is fine, but it is very slowly, because all referenced assemblies will be loaded again and again.
In the second scenario, step 3 - Form1.xaml linked with class Form2 for event handlers.
I can't use another name for xaml file, because this name depends on my buisness logic, I can only change class name (naturally in both xaml file and source file).
I guess the problem is in assembly Short name.
When I change assembly short name (with the same version 1.0.0.0) - All is fine.
It is very strange, because I use assembly strong name, and different version must be treated as different assemblies. I think it is bug.
- This problem with assembly short name is in Beta 2 Feb CTP, I can't check June CTP. May be this bug is already fixed in June CTP.
Hello Eugene,
I know you dealt with this issue a while ago. I'm having the same issue with the Visual Studio 2008 RTM release using the 3.0 framework. Were you able to solve the problem?
- hi BucciAtFisery,
I am running into this problem now. i guess its not just a bug in the beta issue becuase im running vs 2008 rtm release too. did u ever find a solution to the problem? please advice any help is greatly appreciated. thanks in advance.
my favorite place on the net is msdn! i just wanted to post my solution just in case someone else ran into this issue again. since i was working in vs 2008 release version the entry in the assembly file was correct- 1.0.0.0. my issue had nothing to do with the assembly file. i had created a custom control for a user control and then placed that user control in another project. all these files were in one solution. i had dded a reference of the custom control to my user control project and then added a reference to the user control on my main project. i assumed that since i had added the reference of the custom control to the user control project that it was hosted on that it would be sufficient. but it wasnt. i kep getting this load error in the main project. so i ended up adding a reference to the custom controls projects to the main projects reference and that did trick. hope this helps ppl.
my favorite place on the net is msdn!- 답변으로 제안됨Justin Kalweit 2008년 6월 10일 화요일 오후 11:51
- thnaks for your help!
I got the same problem and solved it with your advice.
darioa - thanks! I had this problem too, and you solved it.
- Thank you so much for your post! I had the exact same problem: abstracted a user control out of a main project, had the issue, and adding a (seemingly unnecessary) reference to my UserControl project in my main app and it works now.
I've got an App -> Dialogs -> Controls project set....App depended on Dialogs, Dialogs on Controls....now App & Dialogs both directly reference Controls.
I can stop pulling my hair out for a while now. :) - so glad to help :)
my favorite place on the net is msdn! - check this - http://infosysblogs.com/microsoft/2008/08/wpf_appdomain_cannot_find_type.html
MVP Client App - Thanks a lot for the suggesition.

