Desktop support for .NET Core 3.0 has been in preview for some time, it is slated to be released later this year. As per indicators by Microsoft, .NET Core will receive more updates compared to full .NET Framework. Keeping this in mind, it makes sense to start thinking about migrating existing applications and creating new ones on .NET Core if the application still has some years to go.
Desktop support in .NET Core has not been completely migrated yet; however, it is possible to create and migrate WinForms applications using the preview bits.
In this article, we'll create and migrate a WinForms application in .NET Core 3.0 using ComponentOne controls.
Creating a New .NET Core 3.0 Application
Make sure that you’ve installed the .NET Framework 4.7.2 and .NET Core 2.1 development tools in the Visual Studio Installer. This software is necessary whether you plan on creating a new project or migrating an existing one to .NET Core 3.0.
One way to work with .NET Core 3.0 (for now) is to use dotnet.exe command line utility. It can be used to create a new project, add/restore dependencies, build, etc. But it is the simplest way is to use Visual Studio 2019 and it’s new Windows Forms App (.NET Core) project template.
Using Visual Studio x64 and x86 Versions
Visual Studio uses the appropriate version depending on your .NET Core 3 project’s target platform (x86 or x64). It uses last installed version for both platforms. So, if you intend to build your project for both platforms you must download and install both platform SDK versions with the same version.
These are the typical paths for .NET Core 3 command line utility:
- c:\Program Files\dotnet\dotnet.exe - for x64 version of .Net Core 3
- c:\Program Files (x86)\dotnet\dotnet.exe- for x86 version of .Net Core 3
Create a New Project
First, you’ll need to open the Visual Studio 2019 and create a new Windows Forms App for .NET Core with name:
You should see something like this after the new project has been created:
.NET Core 3.0 uses a new project type and has no design-time support currently, so the process of adding controls will be a little different than what you may be accustomed to and have used in the past. We need to add C1 libraries manually to the project to make use of them. Also, we should add the controls manually to Form1.cs because of missing design-time support.
Because of this, let’s use a trick. We'll add the classic .NET Framework WinForms project in order to use its Form designer:
- Right-click on Solution node and Add – New Project… - Windows Forms Desktop project (with .Net Framework, say version 4.0). The name of new project is by default WindowsFormsApp1:
- Remove the existing Form1 from WindowsFormsApp1 project
- Add Form1.cs from TestAppWinFormsCore project as link:
- After that the following view of Solution Explorer appears:
Now we can edit Form1 form as usual. We can add some C1 control, for example C1DockingTab:
Now you can launch WindowsFormsApp1 application and observe C1DockingTab on Form1.
But this will be a .Net Framework 4 application.
Adding C1DockingTab caused adding C1.Win.C1Command.4 reference to WindowsFormsApp1 project. Therefore, the same reference should be added to TestAppWinFormsCore project also.
For this, let’s take a look in the properties of the C1.Win.C1Command.4 reference (right-click – Properties…) and find the path to the assembly.
It looks like C:\Program Files (x86)\ComponentOne\WinForms Edition\bin\v4.0\C1.Win.C1Command.4.dll.
Add this reference to the TestAppWinFormsCore project (right-click on Dependencies – Add reference - Browse… - choose C:\Program Files (x86)\ComponentOne\WinForms Edition\bin\v4.0\C1.Win.C1Command.4.dll):
You should be able to run the code at this point, though you’ll notice a ComponentOne nag screen since theTestAppWinFormsCore project doesn’t contain any licensing information.
We can correct this by adding a licenses.licx file to the project. To do so, right-click on TestAppWinFormsCore project and select Add -> Existing Items. And browse the licenses.licx file from WindowsFormsApp1/Properties folder.
Build and rerun the app and you should no longer see a nag screen.
Now this is a genuine .NET Core 3.0 WinForms app. You can see this in the Modules window. All system references are from C:\Program Files\dotnet... folder.
Migrating an Existing Project to .NET Core 3.0
It is possible to migrate an existing project to .NET Core 3.0, though the process may require a few more steps than you would expect. Also, before trying to migrate a project, it’s worth running Microsoft’s portability analyzer tool on the project you want to convert.
The tool can give you an idea (ahead of time) how compatible your project will be with .NET Core 3.0, and it points out potential problems you may run into (included unsupported APIs).
Let’s look at the WeatherChart sample migration. This sample demonstrates the C1FlexChart control possibilities.
First, we’ll create a new DotNetCore3FlexChart project (Windows Forms App (.NET Core) in Visual Studio 2019 by the same way as described above.
After that, add the WeatherChart project to the DotNetCore3FlexChart solution. WeatherChart project is placed in Documents\ComponentOne Samples\WinForms\C1FlexChart\CS\WeatherChart\WeatherChart\WeatherChart.csproj by default.
Here, we'll use the WeatherChart project in our .NET Core 3.0 project. Therefore, the WeatherChart project reference should be added to DotNetCore3FlexChart dependencies (right-click on Dependencies – Add Reference… - Projects - WeatherChart)
After that, open Project.cs from DotNetCore3FlexChart project and change the following line:
Application.Run(new Form1()); by Application.Run(new WeatherChart.Form1());
It means that the .NET Core 3.0 app (DotNetCore3FlexChart) will run WeatherChart.Form1 from WeatherChart assembly instead of own Form1.
Now if you run the WeatherChart project it will be launched as .NET Framework 4.0 app. But if you run the DotNetCore3FlexChart project then the WeatherChart assembly will be launched in .NET Core 3.0 environment.
.NET Core 3.0 Preview Caveats
Using the .NET Core 3.0 Preview for any kind of day-to-day work isn’t recommended at this point, as it’s still work in progress. As the steps above illustrate, much of the project creation and migration processes require a lot of manual configuration from the user, and it’s possible to find some bugs and unfinished implementations presently.
The lack of designer support also makes working with the preview somewhat difficult, though Microsoft has committed to adding this feature in the coming months. Overall, .NET Core 3.0 will be an important change for developers, though this preview is merely the first step.
If you have any questions about this process, please leave a comment below.
Try ComponentOne Studio Download the latest version of ComponentOne Studio Enterprise
Try ComponentOne Studio
Download the latest version of ComponentOne Studio EnterpriseDownload Now!
Get Xamarin controls from ComponentOne Download the latest version of ComponentOne Studio for Xamarin
Get Xamarin controls from ComponentOne
Download the latest version of ComponentOne Studio for XamarinDownload Now!