Moin,
ich versuche gerade den ColorTranslator zu benutzen und laufe in einen Fehler, den ich nicht wegbekomme.
The type 'ColorTranslator' exists in both 'System.Drawing.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' and 'System.Drawing.Primitives, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' [WerkstattOrganizer]csharp(CS0433)
Das ist der Code:
using Avalonia.Media;
namespace WerkstattOrganizer
{
public static class Utils
{
public static Color HexToColor(string hex)
{
var color = System.Drawing.ColorTranslator.FromHtml(hex);
return Color.FromRgb(color.R, color.G, color.B);
}
}
}
Das Ding ist jetzt, wenn ich "System.Drawing.Common.ColorTranslator" oder "System.Drawing.Primitives.ColorTranslator" nehme, wird mir gesagt, daß diese Namespaces nicht existieren. Wie wähle ich jetzt eins von beiden aus?
Erster Google Treffer sagt nach 2 Sekunden, dass Du ein veraltetes Paket nimmst
https://github.com/dotnet/core/issues/3244
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Naja, soweit war ich auch schon...
Nun, ich nutze nicht .Net Core 3.0 sondern v6. Alles vorgestern frisch installiert. Muss ich trotzdem jetzt 3.0 installieren?
Dann mach Dir doch ein Testprojekt, wie man es in solchen Fällen macht..?
Ich hab gerade ein .NET 6 Projekt erstellt und habe wie im Issue beschrieben das NuGet hinzufügt.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>
</Project>
zusammen mit Deinem Code (Ausnahme, kein FromRgb genommen, weils das unter System.Drawing.Common
nicht mehr gibt), funktioniert das einwandfrei.
Welches Color.FromRgb Du hier nimmst ist anhand Deines Codes nicht ersichtlich.
using System.Drawing;
namespace WerkstattOrganizer
{
public static class Utils
{
public static Color HexToColor(string hex)
{
var color = ColorTranslator.FromHtml(hex);
return color;
}
}
}
Dein Fehler ist also wo anders.
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Hab übersehen, dass Du Avalonia verwendest, aber auch das funktioniert einwandfrei.
<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="Avalonia" Version="0.10.13" />
</ItemGroup>
namespace WerkstattOrganizer
{
public static class Utils
{
public static Avalonia.Media.Color HexToColor(string hex)
{
System.Drawing.Color color = System.Drawing.ColorTranslator.FromHtml(hex);
return Avalonia.Media.Color.FromRgb(color.R, color.G, color.B);
}
}
}
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Ok, das hat funktioniert. Vielen Dank. Habe nur die Zeile
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
hinzugefügen müssen. Kannte ich so aber auch noch nicht.
Tue mir noch etwas schwer. Ist mein erstes C#-Projekt unter Linux und mit VS Code.
In dem Code kommt Color.FromRPG von Avalonia.Media.
Kannte ich so aber auch noch nicht.
So referenziert man NuGet Pakete - so geht (bzw. sollte) man in .NET mit allen Arten von Abhängigkeiten um, sowohl in .NET Core 1-3, .NET 5-6 wie auch mittlerweile mit .NET 4.x.
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code