Is it possible to make desktop GUI application in .NET Core?

后端 未结 15 1786
我在风中等你
我在风中等你 2020-12-12 11:02

I have been developing Windows Forms programs for few years. I am now looking into .NET Core (including ASP.NET Core MVC). I am searching for the new GUI desktop technology.

相关标签:
15条回答
  • 2020-12-12 11:10

    tl;dr - I'm not sure that it would be possible for the .NET Core developers to supply a cross-platform GUI framework.

    I feel like expecting a cross-platform GUI framework to be bundled into the official tooling (especially an old version of the tooling - you mention that you're running Visual Studio 2015 update 3) for an early version of .NET Core is a little premature.

    GUI frameworks are really quite heavy, and dependent on the hardware abstractions already present on the host machine. On Windows, there is generally a single window manager (WM) and desktop environment (DE) used by most users, but on the many different distributions of Linux which are supported, there are any number of possible WMs and DEs - granted most users will either be using X-Server or Wayland in combination with KDE, GNOME or Xfce. But no Linux installation ever is the same.

    The fact that the open source community can't really settle on a "standard" setup for a VM and DE means that it would be pretty difficult for the .NET Core developers to create a GUI framework which would work across all platforms and combinations of DEs and WMs.

    A lot of folks here have some great suggestions (from use ASP.NET Core to builds a Web application and use a browser to listing a bunch of cross-platform frameworks). If you take a look at some of the mentioned cross platform GUI frameworks listed, you'll see how heavy they are.

    However, there is light at the end of the tunnel as Miguel de Icaza showed off Xamarin running naively on Linux and macOS at .NET Conf this year (2017, if you're reading this in the future), so it might be worth trying that when it's ready.

    (But you'll need to upgrade from Visual Studio 2015 to Visual Studio 2017 to access the .NET Core 2.0 features.)

    0 讨论(0)
  • 2020-12-12 11:11

    If you are using .NET Core 3.0 and above, do the following steps and you are good to go: (I'm going to use .NET Core CLI, but you can use Visual Studio too):

    1. md MyWinFormsApp optional step
    2. cd MyWinFormsApp optional step
    3. dotnet new sln -n MyWinFormsApp optional step, but it's a good idea
    4. dotnet new winforms -n MyWinFormsApp I'm sorry, this is not optional
    5. dotnet sln add MyWinFormsApp do this if you did step #3

    Okay, you can stop reading my answer and start adding code to the MyWinFormsApp project. But if you want to work with Form Designer, keep reading.

    1. Open up MyWinFormsApp.csproj file and change <TargetFramework>netcoreapp3.1<TargetFramework> to <TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks> (if you are using netcoreapp3.0 don't worry. Change it to <TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>)
    2. Then add the following ItemGroup
      <ItemGroup Condition="'$(TargetFramework)' == 'net472'">
        <Compile Update="Form1.cs">
          <SubType>Form</SubType>
        </Compile>
        <Compile Update="Form1.Designer.cs">
          <DependentUpon>Form1.cs</DependentUpon>
        </Compile>
      </ItemGroup>
    

    After doing these steps, this is what you should end up with:

    <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
    
      <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
        <UseWindowsForms>true</UseWindowsForms>
      </PropertyGroup>
    
      <ItemGroup Condition="'$(TargetFramework)' == 'net472'">
        <Compile Update="Form1.cs">
          <SubType>Form</SubType>
        </Compile>
        <Compile Update="Form1.Designer.cs">
          <DependentUpon>Form1.cs</DependentUpon>
        </Compile>
      </ItemGroup>
    
    </Project>
    
    1. Open up file Program.cs and add the following preprocessor-if
    #if NETCOREAPP3_1
        Application.SetHighDpiMode(HighDpiMode.SystemAware);
    #endif
    

    Now you can open the MyWinFormsApp project using Visual Studio 2019 (I think you can use Visual Studio 2017 too, but I'm not sure) and double click on Form1.cs and you should see this:

    Okay, open up Toolbox (Ctrl + W, X) and start adding controls to your application and make it pretty.

    You can read more about designer at Windows Forms .NET Core Designer.

    0 讨论(0)
  • 2020-12-12 11:12

    Yes, it is possible.

    .NET Core doesn't have any components for native GUI application out of the box. However, there is a NuGet package for it that is called Electron.NET, as per Gregor Biswanger's answer.

    Electron is a framework that allows you to build native GUI applications on top of Node.js. Electron.NET is a NuGet package that allows you to utilise Electron and Node.js from within your .NET Core code.

    The good news is that you don't have to learn JavaScript, Electron or Node.js in order to be able to use the NuGet package. JS files do run inside your application, but they get automatically generated by the build process.

    All you do is build a pretty standard ASP.NET Core MVC app. The only difference is that, instead of running in the browser, it runs as a native windowed app. Besides just a few lines of code specific to the Electron.NET package, you won't need to learn anything above ASP.NET Core MVC.

    This page provides a tutorial on how to use it. It also contains some links to sample code repositories.

    0 讨论(0)
  • 2020-12-12 11:12

    You could develop a web application with .NET Core and MVC and encapsulate it in a Windows universal JavaScript app: Progressive Web Apps on Windows

    It is still a web application, but it's a very lightweight way to transform a web application into a desktop app without learning a new framework or/and redevelop the UI, and it works great.

    The inconvenience is unlike Electron or ReactXP for example, the result is a universal Windows application and not a cross platform desktop application.

    0 讨论(0)
  • 2020-12-12 11:14

    .NET Core 3 will have support for creating Windows desktop applications. I watched a demo of the technology yesterday during the .NET Conference.

    This is the only blog post I could find, but it does illustrate the point: .NET Core 3 and Support for Windows Desktop Applications

    0 讨论(0)
  • 2020-12-12 11:17

    You could use Electron and wire it up with Edge.js resp. electron-edge. Edge.js allows Electron (Node.js) to call .NET DLL files and vice versa.

    This way you can write the GUI with HTML, CSS and JavaScript and the backend with .NET Core. Electron itself is also cross platform and based on the Chromium browser.

    0 讨论(0)
提交回复
热议问题