WPF The refrence is invalid or unsupported

hossein tavakoli 471 Reputation points
2022-12-17T02:29:41.16+00:00

Hi dears,
I want to add System.Windows.Forms.dll to my project but it get an error The refrence is invalid or not supported.
my project : WPF .net 6/C#/32bit

I want to use WindowsFormsHost control

<PropertyGroup>  
    <OutputType>WinExe</OutputType>  
    <TargetFramework>net6.0-windows</TargetFramework>  
    <UseWPF>True</UseWPF>  
    <UseWindowsForms>True</UseWindowsForms>  
    <ApplicationIcon>AppIcon.ico</ApplicationIcon>  
    <PlatformTarget>x86</PlatformTarget>  
    <PackageIcon></PackageIcon>  
    <ApplicationManifest>app.manifest</ApplicationManifest>  
    <RunAnalyzersDuringBuild>True</RunAnalyzersDuringBuild>  
  </PropertyGroup>  
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,830 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,262 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,256 Reputation points Microsoft Vendor
    2022-12-19T09:56:21.39+00:00

    Hi, @hossein tavakoli . Welcome Microsoft Q&A.
    I am testing with the code below and it works. Could the error be caused by something else?

    <Project Sdk="Microsoft.NET.Sdk">  
      <PropertyGroup>  
        <OutputType>WinExe</OutputType>  
        <TargetFramework>net6.0-windows</TargetFramework>  
    	  <UseWPF>True</UseWPF>  
    	  <UseWindowsForms>True</UseWindowsForms>  
    	  <PlatformTarget>x86</PlatformTarget>  
    	  <PackageIcon></PackageIcon>  
    	  <RunAnalyzersDuringBuild>True</RunAnalyzersDuringBuild>  
        <Platforms>AnyCPU;ARM32</Platforms>  
      </PropertyGroup>  
    </Project>  
    
    <Grid>  
            <WindowsFormsHost x:Name="host" Width="300" Height="300">  
            </WindowsFormsHost>  
        </Grid>  
     public partial class MainWindow : Window  
        {  
            MaskedTextBox mtb = new MaskedTextBox("00/00/0000");  
            ToolTip tt=new System.Windows.Forms.ToolTip();  
            public MainWindow()  
            {  
                InitializeComponent();  
                mtb.Width = 200;  
                mtb.Height=  100;  
                mtb.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);  
                mtb.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);  
                host.Child = mtb;  
            }  
            void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)    {    ...    }  
            void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)    {  tt.Hide(mtb); }  
        }  
    

    272081-55.png