6

I have a project in Visual Studio 2019 that I am migrating from .NET Framework 4.6 to .NET Core 3.1.

I used the guide from Microsoft to port my project, described here. I ran the Portability Analyzer and it showed, that the project is to 100% portable.

But every dependency to System.Windows doesn't work anymore. For example I use System.Windows.Rect. In the official documentation it clearly states that it is available for .NET Core 3.1.

In the code however it marks Rect with "The type or namespace name 'Rect' could not be found(are you missing a using directive or an assembly reference?)" and on the top it marks my using System.Windows grey with the message "Using directive is unnecessary".

This error applies to many things in my code, like System.Windows.ResourceDictionary, System.Windows.Application.Current, System.Windows.Point, ...

So for some reasons it seems that I can't use System.Windows, even if the using itself seems to work fine.

I searched for System.Windows in the NuGet package manager and googled it, but couldn't find anything.

So here is my question: How can I use System.Windows in a .NET Core 3.1 project with Visual Studio 2019? My guess at the moment is, that I need to manually include the assembly WindowsBase.dll but in my installation directory (C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional) I can only find one at C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\Extensions\Microsoft\LiveShare\Agent and it doesn't seem to be the right one.

izlin
  • 2,129
  • 24
  • 30
  • 1
    In your .proj file, are you using `` or `` ? – Rosdi Kasim Apr 20 '20 at 08:37
  • Did you set `UseWPF=true` property in `csproj` file? – Pavel Anikhouski Apr 20 '20 at 08:38
  • @RosdiKasim I use the default settings with ``. – izlin Apr 20 '20 at 08:42
  • 1
    @PavelAnikhouski I haven't set `UseWPF` in my csproj file, so it is still the default. – izlin Apr 20 '20 at 08:44
  • 2
    As far as graphics Point and Rect is concerned, Microsoft has released System.Drawing.Common to provide access to GDI+ graphics functionality cross-platform. You can install System.Drawing.Common via NuGet. Check if you have installed that lib. – Goodies Apr 20 '20 at 09:04
  • Well..., did you try changing it to `Microsoft.NET.Sdk.WindowsDesktop` and rebuild? – Rosdi Kasim Apr 20 '20 at 16:43
  • Does this answer your question? [How to reference System.Windows.Forms in .NET Core 3.0 for WPF apps?](https://stackoverflow.com/questions/58844785/how-to-reference-system-windows-forms-in-net-core-3-0-for-wpf-apps) – JumpingJezza Dec 17 '21 at 08:08

2 Answers2

10

Here is how to do it with net5.0:

 <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
Daniel Fisher lennybacon
  • 3,865
  • 1
  • 30
  • 38
2

Based on your description, you would like to use System.Windows.Rect in in a .NET Core 3.1

project with Visual Studio 2019. I find a solution and you can try the following steps.

First, we need to Create a .net core 3.1 console app.

Second, we need to set UseWPF=true property in csproj file.

 <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
         <UseWPF>true</UseWPF>
  </PropertyGroup>

Thirdly, we need install nuget package System.Runtime.WindowsRuntime.

Fourth, rebuild the app and write the following code.

using Windows.Foundation;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Rect myRect1 = new Rect();
            myRect1.X = 10;
            myRect1.Y = 100;
            myRect1.Width = 150;
            myRect1.Height = 100;

        }
    }
}

Besides, If you want to use system.windows in .net core 3.1, I suggest that you use WPF

.net core 3.1 app.

Jack J Jun
  • 5,633
  • 1
  • 9
  • 27
  • This approach is not recommended by your employer Microsoft. The right one is to create a WPF .NET Core project by running `dotnet new wpf`. – Lex Li Apr 23 '20 at 02:39