使用.net 6.0框架的WPF应用如何引用System.Windows.Forms这个dll

在.net 6.0的WPF应用中,想使用OpenFileDialog 这个类或者FolderBrowserDialog这个类,是无法找到System.Windows.Forms这个dll引用的,即使从系统C盘里搜索到System.Windows.Forms.dll放到项目里,也会出现异常,而无法识别WPF自带的一些初始化方法。

OpenFileDialog还好,因为他有两个命名空间都有它,一个是Microsoft.Win32,一个是System.Windows.Forms,无法引用System.Windows.Forms还可以退而求其次使用 using Microsoft.Win32;

以前的.net框架直接在Visual Studio项目->引用->右键->添加引用,可以直接方便的找到System.Windows.Forms.dll,但是.net 6.0不可以。经过一番搜索,终于在

Is it possible to use FolderBrowserDialog in a WPF Net 6 application?

找到解决方案

解决方案

<UseWindowsForms>true</UseWindowsForms>

放置的位置是,【项目名】.csproj文件,在Visual Studio 2022 里双击项目名可以打开这个文件。

 <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWPF>true</UseWPF>
    <AssemblyVersion>1.0.1</AssemblyVersion>
    <FileVersion>1.0.1.0</FileVersion>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

希望对你有所帮助,天下程序员是一家。

In .NET 6 and later versions, the `System.Windows.Forms` namespace is not included in the default installation of WPF applications. However, you can still use the `FolderBrowserDialog` class in your WPF application by adding a reference to the `System.Windows.Forms` NuGet package and using the `System.Windows.Forms.Integration` namespace. Here are the steps to use `FolderBrowserDialog` in .NET 6 WPF: 1. Add a reference to the `System.Windows.Forms` NuGet package in your WPF project. 2. Add the following using statements to the top of your code file: ```csharp using System.Windows.Forms; using System.Windows.Forms.Integration; ``` 3. In your code, create a `FolderBrowserDialog` object and use the `ShowDialog` method to display the dialog box: ```csharp FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { // do something with the selected folder } ``` 4. If you want to use the `FolderBrowserDialog` in a WPF window or page, you can use the `WindowsFormsHost` control to host the dialog box: ```xml <Window xmlns:winforms="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" ...> <Grid> <winforms:WindowsFormsHost> <forms:FolderBrowserDialog x:Name="folderDialog" /> </winforms:WindowsFormsHost> </Grid> </Window> ``` Then, you can use the `ShowDialog` method to display the dialog box: ```csharp if (folderDialog.ShowDialog() == DialogResult.OK) { // do something with the selected folder } ``` Note that you will also need to add a reference to the `WindowsFormsIntegration` assembly in your WPF project.
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值