If you’ve heard the phrase, “Once you go Mac, you never go back,” you know it holds true. Macs offer amazing features, apps, and top-notch desktop programs supplied by Apple. But there are. Running Windows software on a Mac. To run your app once installation has completed, open Crossover Office if it isn’t already open, and ensure the All Bottles link at the top left is selected.
-->
This article describes how to port your Windows Forms-based desktop app from .NET Framework to .NET Core 3.0 or later. The .NET Core 3.0 SDK includes support for Windows Forms applications. Windows Forms is still a Windows-only framework and only runs on Windows. This example uses the .NET Core SDK CLI to create and manage your project.
In this article, various names are used to identify types of files used for migration. When migrating your project, your files will be named differently, so mentally match them to the ones listed below:
Prerequisites
Note
.NET Core 3.0 projects are only supported in Visual Studio 2019 or a later version. Starting with Visual Studio 2019 version 16.5 Preview 1, the .NET Core Windows Forms designer is also supported.
To enable the designer, go to Tools > Options > Environment > Preview Features and select the Use the preview Windows Forms designer for .NET Core apps option.
Consider
When porting a .NET Framework Windows Forms application, there are a few things you must consider.
Create a new SDK project
The new .NET Core 3.0 project you create must be in a different directory from your .NET Framework project. If they're both in the same directory, you may run into conflicts with the files that are generated in the obj directory. In this example, we'll create a directory named MyFormsAppCore in the SolutionFolder directory:
Next, you need to create the MyFormsCore.csproj project in the MyFormsAppCore directory. You can create this file manually by using the text editor of choice. Paste in the following XML:
If you don't want to create the project file manually, you can use Visual Studio or the .NET Core SDK to generate the project. However, you must delete all other files generated by the project template except for the project file. To use the SDK, run the following command from the SolutionFolder directory:
After you create the MyFormsCore.csproj, your directory structure should look like the following:
Add the MyFormsCore.csproj project to MyApps.sln with either Visual Studio or the .NET Core CLI from the SolutionFolder directory:
Fix assembly info generation
Windows Forms projects that were created with .NET Framework include an
AssemblyInfo.cs file, which contains assembly attributes such as the version of the assembly to be generated. SDK-style projects automatically generate this information for you based on the SDK project file. Having both types of 'assembly info' creates a conflict. Resolve this problem by disabling automatic generation, which forces the project to use your existing AssemblyInfo.cs file.
There are three settings to add to the main
<PropertyGroup> node.
Add these three elements to the
<PropertyGroup> node in the MyFormsCore.csproj file:
Add source code
Right now, the MyFormsCore.csproj project doesn't compile any code. By default, .NET Core projects automatically include all source code in the current directory and any child directories. You must configure the project to include code from the .NET Framework project using a relative path. If your .NET Framework project used .resx files for icons and resources for your forms, you'll need to include those too.
Add the following
<ItemGroup> node to your project. Each statement includes a file glob pattern that includes child directories.
Alternatively, you can create a
<Compile> or <EmbeddedResource> entry for each file in your .NET Framework project.
Add NuGet packages
Add each NuGet package referenced by the .NET Framework project to the .NET Core project.
Most likely your .NET Framework Windows Forms app has a packages.config file that contains a list of all of the NuGet packages that are referenced by your project. You can look at this list to determine which NuGet packages to add to the .NET Core project. For example, if the .NET Framework project referenced the
MetroFramework , MetroFramework.Design , and MetroFramework.Fonts NuGet packages, add each to the project with either Visual Studio or the .NET Core CLI from the SolutionFolder directory:
The previous commands would add the following NuGet references to the MyFormsCore.csproj project:
How To Install Windows Form App For Mac![]() Port control libraries
If you have a Windows Forms Controls library project to port, the directions are the same as porting a .NET Framework Windows Forms app project, except for a few settings. And instead of compiling to an executable, you compile to a library. The difference between the executable project and the library project, besides paths for the file globs that include your source code, is minimal.
Using the previous step's example, lets expand what projects and files we're working with.
Consider the differences between the
MyControlsCore.csproj project and the previously created MyFormsCore.csproj project.
Here is an example of what the .NET Core Windows Forms Controls library project file would look like:
Windows Form Application Mac Visual Studio
As you can see, the
<OutputType> node was removed, which defaults the compiler to produce a library instead of an executable. The <AssemblyName> and <RootNamespace> were changed. Specifically the <RootNamespace> should match the namespace of the Windows Forms Controls library you are porting. And finally, the <Compile> and <EmbeddedResource> nodes were adjusted to point to the folder of the Windows Forms Controls library you are porting.
Free Windows For Mac
Next, in the main .NET Core MyFormsCore.csproj project, add a reference to the new .NET Core Windows Forms Control library. Add a reference with either Visual Studio or the .NET Core CLI from the SolutionFolder directory:
The previous command adds the following to the MyFormsCore.csproj project:
Compilation problems
If you have problems compiling your projects, you may be using some Windows-only APIs that are available in .NET Framework but not available in .NET Core. You can try adding the Windows Compatibility Pack NuGet package to your project. This package only runs on Windows and adds about 20,000 Windows APIs to .NET Core and .NET Standard projects.
![]()
The previous command adds the following to the MyFormsCore.csproj project:
Windows Forms Designer
As detailed in this article, Visual Studio 2019 only supports the Forms Designer in .NET Framework projects. By creating a side-by-side .NET Core project, you can test your project with .NET Core while you use the .NET Framework project to design forms. Your solution file includes both the .NET Framework and .NET Core projects. Add and design your forms and controls in the .NET Framework project, and based on the file glob patterns we added to the .NET Core projects, any new or changed files will automatically be included in the .NET Core projects.
Once Visual Studio 2019 supports the Windows Forms Designer, you can copy/paste the content of your .NET Core project file into the .NET Framework project file. Then delete the file glob patterns added with the
<Source> and <EmbeddedResource> items. Fix the paths to any project reference used by your app. This effectively upgrades the .NET Framework project to a .NET Core project.
Next steps
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |