问题描述
我正在将旧的 .NET Framework 4 WinForms 应用程序转换为 .NET 5.在这种情况下,最简单的方法是重新创建项目,该项目大部分都有效.但是,我有一种方法可以从内存流中解码 JPG 图像:
I'm converting an old .NET Framework 4 WinForms application to .NET 5. In this case, the easiest way was to re-create the project, which mostly worked. However, I have a method to decode a JPG image from a memory stream:
// Look for JFIF header
MemoryStream memStream = new MemoryStream( rawData );
JpegBitmapDecoder decoder = new JpegBitmapDecoder( memStream,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default );
BitmapSource bmpSource = decoder.Frames[0];
Bitmap bitmap = new Bitmap( bmpSource.PixelWidth, bmpSource.PixelHeight,
PixelFormat.Format24bppRgb );
这失败了,因为 VS 2019 抱怨使用 System.Windows.Media.Imaging"导致错误,并且 JpegBitmapDecoder 显示为红色波浪线.如果我将鼠标悬停在 JpegBitmapDecoder 上,我将获得使用 System.Windows.Media.Imaging;"的选项.来自 PresentationCode".
This fails because VS 2019 complains that "using System.Windows.Media.Imaging" causes an error, and JpegBitmapDecoder shows with a red squiggly. If I hover over JpegBitmapDecoder, I'm given the option for "using System.Windows.Media.Imaging; from PresentationCode".
但是,选择它会导致 VS 2019 卡在执行建议的操作"上,试图添加使用.
However, choosing that causes VS 2019 to be stuck on "Execute Suggested Action", trying to add the using.
该项目面向 .NET 5,我添加了 Microsoft.Windows.Compatibility NuGet 包,那么我做错了什么?或者,如何访问 JpegBitmapDecoder?
The project is targeted for .NET 5, and I've added the Microsoft.Windows.Compatibility NuGet package, so what am I doing wrong? Or, how to I access the JpegBitmapDecoder?
推荐答案
注意:.NET 5 项目仅在 Visual Studio 2019 的 16.8.0 版及更高版本中受支持(我才意识到预览"已成为发布状态).
Note: .NET 5 projects are supported only in version 16.8.0 of Visual Studio 2019 and later (I only just realized that the "Preview" had made it to released status).
当我尝试在 Visual Studio 2019 Preview 中重现您的问题时,我遇到了一个稍微不同的问题:当我选择 "using System.Windows.Media.Imaging;从 PresentationCode 菜单项中,出现一个进度对话框:
When I try to reproduce your problem in Visual Studio 2019 Preview, I get a slightly different issue: when I select the "using System.Windows.Media.Imaging; from PresentationCode" menu item, a progress dialog shows up:
这个对话似乎永远持续下去.Visual Studio 似乎从未真正完成该操作.
This dialog seems to go on forever. Visual Studio never seems to actually complete the action.
因此该功能的一部分似乎已损坏.但是,幸运的是,您可以手动执行 Visual Studio 应该为您完成的相同操作.只需选择项目,然后从项目"菜单中选择编辑项目文件",然后添加 到主项目
元素.
So that part of the feature seems broken. However, fortunately you can do the same thing manually that Visual Studio should have done for you. Simply select the project, and then choose "Edit Project File" from the "Project" menu, and add <UseWPF>true</UseWPF>
to the main project <PropertyGroup>
element.
完成后,项目文件可能如下所示:
When you're done, the project file might look something like this:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
此时,项目的Dependencies"中Frameworks"项下的条目Microsoft.WindowsDesktop.App.WindowsForms"被打开. WPF 库应更改为 Microsoft.WindowsDesktop.App",并将包含您需要的 Winforms 和 WPF 库.
At that point, the entry "Microsoft.WindowsDesktop.App.WindowsForms" under the "Frameworks" item in the project's "Dependencies" WPF libraries should change to "Microsoft.WindowsDesktop.App" and will include both the Winforms and WPF libraries that you need.
您也可以采用另一种方式,将 Winforms 支持添加到现有的 WPF 项目,只需添加 <UseWindowsForms/>
元素即可.
You can go the other way as well, adding Winforms support to an existing WPF project, simply by adding the <UseWindowsForms/>
element instead.
当然,这一切都引出了一个问题,为什么您不只使用 GDI+ 图像处理类型.Winforms 可以像 WPF 一样加载 JPEG 文件,所以不清楚为什么要依赖 WPF.但也有其他混合搭配的原因.无论您想在 .NET 5 项目中结合 Winforms 和 WPF 的原因是什么,以上都可以做到.
Of course, this all begs the question why you don't just use the GDI+ image handling types. Winforms can load JPEG files just as well as WPF, so why you want the dependency on WPF is unclear. But there are other reasons to mix and match too. Whatever the reason you want to combine Winforms and WPF in a .NET 5 project, the above will do it.
这篇关于如何将 WPF 支持添加到 .NET 5 Winforms 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!