MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > LoadComponent - Beta2 --> June CTP
發問發問
 

已答覆LoadComponent - Beta2 --> June CTP

  • 2006年7月12日 下午 11:49Derek Ekins 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:im="clr-namespace:MyNameSpace"
      >
      <ControlTemplate x:Key="MyKey">
        <im:MyCustomControl Width="100"/>
      </ControlTemplate>
    </ResourceDictionary>
    MyNameSpace exists within MyPluginAssembly and has a custom control called MyCustomControl.

    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.

解答

  • 2006年7月21日 下午 04:39Ashish Shetty - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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.

所有回覆

  • 2006年7月21日 上午 09:40Derek Ekins 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Any ideas?
    I still haven't been able to solve this.
  • 2006年7月21日 下午 04:39Ashish Shetty - MSFT版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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.
  • 2006年7月24日 上午 08:26Derek Ekins 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Yes this works. Thanks very much!
  • 2006年7月27日 上午 07:38Eugene S.B_ 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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.

  • 2006年7月27日 下午 10:56Rob Relyea版主使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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 PM

  • 2006年7月28日 上午 06:47Eugene S.B_ 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    First 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.

  • 2006年7月28日 上午 06:52Eugene S.B_ 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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).

  • 2006年7月28日 上午 07:56Eugene S.B_ 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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.

  • 2006年7月28日 上午 11:18Eugene S.B_ 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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.
  • 2008年2月6日 下午 12:50BucciAtFiserv 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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?

  • 2008年6月4日 下午 06:33msdner1 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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!
  • 2008年6月6日 下午 02:03msdner1 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     提議的解答

    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!
  • 2008年6月10日 上午 01:24darioa123 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    thnaks for your help!
    I got the same problem and solved it with your advice.
    darioa
  • 2008年6月10日 下午 11:51Justin Kalweit 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    thanks! I had this problem too, and you solved it.
  • 2008年7月17日 下午 08:57Nunyo Biznatch 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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. :)
  • 2008年7月31日 下午 06:13msdner1 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    so glad to help :)
    my favorite place on the net is msdn!
  • 2008年8月21日 上午 09:03Atul GuptaMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
  • 2009年1月28日 上午 09:00zy_deutschland 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Thanks a lot for the suggesition.