none
Why ilmerge doesn't work if a dll file contains a xaml user control?

    Question

  • To reproduce the error, follows the step below.

    1) Create a WPF User Control Library.

    2) Create a default user control as follow 


    <UserControl x:Class="WpfControlLibrary1.UserControl2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid>
            <Label Content="UserControl2.xaml"/>
        </Grid>
    </UserControl>


    3) use ilmerge to create a test library.

    ilmerge /lib /out:wpfTestLib.dll WpfUserControlLibrary1.dll

    4) Add the wpfTestLib.dll to the reference of another Wpf window application and add the UserControl2 custom control.

    <Window x:Class="TestLib.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:WpfControlLibrary1;assembly=wpfTestLib"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <c:UserControl2/>
        </Grid>
    </Window>

    5) You will get the following compiler error.

    Could not create an instance of type 'UserControl2'.


    I am using vs2008 and I have downloaded the latest version of ilmerge. Thus I wonder what went wrong?  
    • Edited by Sarah97 Wednesday, December 30, 2009 3:50 AM
    Tuesday, December 29, 2009 8:40 PM

All replies

  • You have your xml namespace defined as:

        xmlns:c="clr-namespace:WpfControlLibrary1;assembly=tcTestLib"

    This is saying to pull it from the assembly "tcTestLib.dll".

    However, with ILMerge, you created the assembly "wpfTestLib.dll"

    You'll need to change the corresponding XAML declaration, or it will not find the appropriate control.

    Reed Copsey, Jr. - http://reedcopsey.com
    Tuesday, December 29, 2009 8:53 PM
  • Hi Reed,

    Thanks for your reply. I made a typing mistake. Even thought I change from tcTestLib to wpfTestLib. The error is still there. What seems very strange is that the intellisense of vs2008 xaml editor shows "UserControl2" right after I finished typing "c:" which means the intellisense knows where to find the custom user control. However I just can't get it to compile.




    Wednesday, December 30, 2009 3:53 AM
  • I forget to mention that I am using the .net 3.5 framework. Anyone has any idea why there is the error "Could not create an instance of type 'UserControl2'" Especially all the codes are auto-generated. I didn't add any of my own codes. 
    Wednesday, December 30, 2009 4:36 PM
  • Anyone has any idea? Is this a bug?
    Thursday, December 31, 2009 4:53 PM
  • Does anyone have been able to reproduce the same compiler error or it's just me that has such wired error?
    Sunday, January 03, 2010 6:03 AM
  • Hi Everyone,

    I have tried this on my xpbox, it gives me the same compiler error. Any help?
    Monday, January 04, 2010 5:38 PM
  • Is this a bug?
    Thursday, January 07, 2010 7:55 PM
  • could anyone tell me if this is a bug? 
    Monday, January 11, 2010 5:18 PM
  • Hi Microsoft team,

    I am not trying to be difficult. The reason I want to find out whether is this is a bug is that I have a lot of xaml user controls which are all in separated project files.  I simply want to find out if this is a bug or, may be, there is a simple solution to fix it.

    thanks
    Friday, January 15, 2010 7:16 PM
  • Sarah,

    I've come across this problem myself in recent days.  From a brief bit of searching it seems ILMerge doesn't work flawlessly with WPF as it can't handle xaml resources.


    Regards,
    Matt
    Monday, January 18, 2010 8:23 PM
  • Matt,

    Thanks for your reply. I guess I am not the only one having this problem. I guess Microsoft WPF and ILMerge teams should take note it. Personally I think this is a bug.

    Best wish
    Sarah
    Friday, February 12, 2010 3:39 AM
  • I don't know if you are still having a problem with this - I sort of am.  I found that anything saved as a resource seems to appear in the new assembly under the oldassemblyname.g.resources.  Take a look in reflector.

    Normally you'd have the following:

    <oldAssemblyName>.dll:  \Resources\<oldAssemblyName>.g.resources

    after the merge you have:

    <newAssemblyName>.dll:  \Resources\<oldAssemblyName>.g.resources

    I've been able to load the XAML resources out (resource dictionary) by using the following:

     

    Stream stream = assembly.GetManifestResourceStream(oldAssemblyName+".g.resources");

     

    //Read the found resource

     

    //Find the correct key in the resource

     

    using (ResourceReader resourceReader = new ResourceReader(stream))

    {

     

       foreach (DictionaryEntry de in resourceReader)

       {

     

          if (string.Compare((string)de.Key, ResourcePath, true) == 0)

         {

            rd = (

    ResourceDictionary)XamlReader.Load((Stream)de.Value);

     

           break;

         }

       }

    }

    Where the ResourcePath is <oldAssemblyName>.g.resource.  I do however still have 1 problem.  I have a reference to a view model inside my resource dictionary, and this bombs out when loading the resource dictionary.  I've added the schema as:

    [

    assembly: XmlnsDefinition("http://schemas/ViewModels", "ResourceSupplierDLL.ViewModels")]

    Now, it's the way it bombs out that is funny.  If I put my ResourceSupplierDLL.DLL as the first assembly to merge in ILMerge - it works.  But if it's anywhere but the first I get the exception public type TestViewModel cannot be found - even though after loading the assembly the type does exist (checked it out by calling assembly.GetTypes() on my just loaded assembly).

     

    Very odd problem indeed.


    Ben
    Sunday, June 27, 2010 1:10 PM