locked
Hosting a WinForms 2.0 control in a WPF 4 project RRS feed

  • Question

  • I've got a WinForms assembly which targets .NET 2.0. I want to use one of the controls in my WPF 4 application. When I add the assembly as a reference, I am able to write code and intellisense has no issues.

    Then I compile.

    After attempting to compile, intellisense acts like the assembly is not referenced ("Type 'X.X.X' is not defined."). Is this an issue with trying to use a WinForms control in WPF (WindowsFormHost) or is this an issue with trying to mix and match assemblies which target different versions of .NET?

    I've tried instantiating my WinForms control both declaratively and from the "code behind" without success.

    Thanks

    Tuesday, August 9, 2011 7:19 PM

Answers

All replies

  • Hi,

    Hosting a WinForms control should not cause this problem. It could be a missing dependency of your WinForms assembly. Are you sure you have included all the references? Or does the WinForms assembly define the "missing" type?

    HTH


    -Scosby
    Microsoft Community Contributor
    Wednesday, August 10, 2011 2:41 AM
  • Hi,

    What is the "XXX" type not defined, could you please check if you import the assemblies of your winform control. And post some code here, then we could help you to check it.

    And please follow this article to enablethe useLegacyV2RuntimeActivationPolicy (False is the default value). http://msdn.microsoft.com/en-us/library/bbx34a2h.aspx

    Create a app.config for your project, and code as:

    <?xml version="1.0"?>
    <configuration>
      <startup useLegacyV2RuntimeActivationPolicy="True">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>

     

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 10, 2011 6:41 AM
  • The problem is more complicated then I thought it was.

    Turns out that while I'm adding a reference to the necessary assemblies, Visual Studio is not copying the assemblies locally (even though I've specified I want them copied locally). When I add the assembly for the first time, intellisense spots the types I want and everything is ok. But when I try to compile, it's as if Visual Studio is unloading the assemblies (even though I can still see them in my references list). They are not copied and intellisense no longer recognizes the types - I can't even see the root namespaces in the Object Browser.

    The odd thing is that some of my other assemblies which also target the 2.0 runtime are visible - so it's not a compatibility issue between versions 2 and 4.

    I'm stumped.

    Wednesday, August 10, 2011 1:23 PM
  • Please enable your build output and view the log in the output panel while building your application. VS Tools Menu > Options... > Projects and Solutions > Build and Run > Select Detailed for the MSBuild project build output verbosity.

    Then share the output here, and let us to check the actions during the building.

    If it is not a issue of the compatibility between CLR 2 and CLR 4, there the MSBuild may do something in your project.

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, August 11, 2011 10:38 AM
  • Ah yess.... the build output. I always forget just how helpful that output can be.

     

    ------ Build started: Project: RdbPlugins, Configuration: Debug Any CPU ------
    C:\MyProject\MyWindow.xaml.vb(35) : error BC30002: Type 'ReferencedAssembly.ReferencedType' is not defined.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "DevExpress.XtraEditors.v10.2.Design, Version=10.2.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "ReferencedAssembly1, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b79c5a4633cc8726, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "ReferencedAssembly2, Version=2.0.0.0, Culture=neutral, PublicKeyToken=e0e7d9107d5e684d, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "ReferencedAssembly3, Version=2.0.0.0, Culture=neutral, PublicKeyToken=78ea3873141a18c3, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "ReferencedAssembly4, Version=2.0.0.0, Culture=neutral, PublicKeyToken=1212c1cffa876ebd, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "DevExpress.XtraEditors.v10.2.Design, Version=10.2.8.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1360,9): warning MSB3253: The referenced assembly "ReferencedAssembly5, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0a412baa1e119491, processorArchitecture=MSIL" could not be resolved because it has a dependency on "System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project.
    ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
    
    Build Summary
    -------------
    00:00.633 - Failed - Debug Any CPU - MyProject.vbproj
    
    Total build time: 00:00.686
    
    ========== Build: 1 succeeded or up-to-date, 1 failed, 0 skipped ==========

    Thursday, August 11, 2011 12:51 PM
  • It seems like your project is targeting the Client profile instead of the normal Framework. Try retargeting the project to ".NET Framework 4" instead of ".NET Framework 4 Client Profile" and see if that helps. The .NET Framework Client Profile is only a subset of features normally in the Framework, which is why your project can't resolve a specific type in the client profile.

    You can read How to: Target a Specific .NET Framework Version of Profile for more information.

    HTH


    -Scosby
    Microsoft Community Contributor
    • Marked as answer by Cyborgx372 Thursday, August 11, 2011 7:19 PM
    Thursday, August 11, 2011 1:35 PM
  • Thanks. What is the Client Profile and why would VS default to it?

    EDIT -

    Never mind: http://msdn.microsoft.com/en-us/library/cc656912.aspx

    Thursday, August 11, 2011 7:19 PM