Skip to content

WindowsFormHost not usable from WPF XAML #2085

Closed

Description

@mjrousos

WindowsFormHost not usable from WPF XAML

XAML using the WindowsFormHost control does not build in .NET Core 3 WPF apps. For example, the following XAML builds for .NET Framework but not for .NET Core 3:

<WindowsFormsHost>  
    <windowsForms:Panel />
</WindowsFormsHost>

@ericstj has pointed out that this is probably because our WindowsFormsIntegration reference assembly is missing necessary XMLNS attributes.

@nguerrera has suggested a work-around (which helped in my case) - explicitly specify WindowsFormsHost's namespace. For example:

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

<wfi:WindowsFormsHost>  
    <windowsForms:Panel />
</wfi:WindowsFormsHost>

Activity

  1. added a commit that references this issue on Nov 28, 2018
  2. added
    area-wpf
    on Nov 29, 2018
  3. ericstj commented on Nov 29, 2018

    @ericstj
    Member

    Fix for this has been checked into WPF yesterday, leaving this open until fix makes it to the SDK

  4. added this to the 3.0.0 milestone on Nov 29, 2018
  5. ericstj commented on Nov 29, 2018

    @ericstj
    Member

    We were missing the XMLNS attribute for System.Windows.Controls.Ribbon and System.Windows.Documents.DocumentStructures as well which were fixed in the same commit.

  6. karelz commented on Dec 18, 2018

    @karelz
    Member

    Seems to be addressed, closing.

  7. virzak commented on Jan 10, 2019

    @virzak

    I got latest daily build but can't resolve System.Windows.Controls.Ribbon when using <TargetFrameworks>net40</TargetFrameworks>.

    Works properly with <TargetFrameworks>net45</TargetFrameworks>

  8. ericstj commented on Jan 10, 2019

    @ericstj
    Member
  9. virzak commented on Jan 11, 2019

    @virzak

    @ericstj is there a way to prevent ribbon it from loading?

    Trying to migrate a library to core which previously targeted 4.0

    https://github.com/ejensen/toggle-switch-control/blob/master/WPF/ToggleSwitch/ToggleSwitch.csproj#L13

  10. ericstj commented on Jan 11, 2019

    @ericstj
    Member

    I see, that sounds like a bug with the Microsoft.NET.Sdk.WindowsDesktop implicit references. You can set DisableImplicitFrameworkReferences to true to disable that, then you have to list your references manually on net4x. I'll file the bug in the WPF repo on the SDK targets.

    Alternatively, just update it to target net45 instead if your end goal is to get it to core.

  11. adamdriscoll commented on Jan 4, 2020

    @adamdriscoll

    I'm running .NET Core SDK 3.1.100 and trying to use WindowsFormsHost from XAML. It's failing to find the tag but from everything mentioned in this thread it should be working. A simple repro:

    Project

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
        <UseWPF>true</UseWPF>
      </PropertyGroup>
    </Project>
    

    XAML

    <Window x:Class="Sample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:Sample"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <WindowsFormsHost></WindowsFormsHost>
        </Grid>
    </Window>
    
    

    Output

    PS C:\src\Sample> dotnet build
    Microsoft (R) Build Engine version 16.4.0+e901037fe for .NET Core
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      Restore completed in 33.43 ms for C:\src\Sample\Sample.csproj.
    C:\src\Sample\MainWindow.xaml(10,10): error MC3074: The tag 'WindowsFormsHost' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 10 Position 10. [C:\src\Sample\Sample.csproj]
    C:\src\Sample\MainWindow.xaml(10,10): error MC3074: The tag 'WindowsFormsHost' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 10 Position 10. [C:\src\Sample\Sample.csproj]
    
    Build FAILED.
    
    C:\src\Sample\MainWindow.xaml(10,10): error MC3074: The tag 'WindowsFormsHost' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 10 Position 10. [C:\src\Sample\Sample.csproj]
    C:\src\Sample\MainWindow.xaml(10,10): error MC3074: The tag 'WindowsFormsHost' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 10 Position 10. [C:\src\Sample\Sample.csproj]
        0 Warning(s)
        2 Error(s)
    
    Time Elapsed 00:00:01.08
    PS C:\src\Sample> dotnet --version
    3.1.100
    

    What am I missing?

  12. ivan2007 commented on Mar 23, 2021

    @ivan2007

    Add <UseWindowsForms>true</UseWindowsForms> in your project file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-wpf

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions