如何在配置中指定颜色

时间:2021-11-08 01:37:19

How would I specify a color in app.config and then convert that into an actual System.Drawing.Color object at runtime?

如何在app.config中指定颜色,然后在运行时将其转换为实际的System.Drawing.Color对象?

6 个解决方案

#1


3  

One way would be to specify one of the KnownColor values as the config text and then use Color.FromName to create the Color object.

一种方法是将一个KnownColor值指定为配置文本,然后使用Color.FromName创建Color对象。

#2


2  

Color is an oddity; regular xml-serialization doesn't normally work - hence you often need to add your own code, perhaps via TypeConverter:

颜色很奇怪;常规的xml-serialization通常不起作用 - 因此您经常需要添加自己的代码,可能通过TypeConverter:

static void Main()
{

    Test(Color.Red);
    Test(Color.FromArgb(34,125,75));
}
static void Test(Color color)
{
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color));
    string s = converter.ConvertToInvariantString(color);
    Console.WriteLine("String: " + s);
    Color c = (Color) converter.ConvertFromInvariantString(s);
    Console.WriteLine("Color: " + c);
    Console.WriteLine("Are equal: " + (c == color));
}

Outputs:

String: Red
Color: Color [Red]
Are equal: True
String: 34, 125, 75
Color: Color [A=255, R=34, G=125, B=75]
Are equal: True

#3


2  

You config would look like this:

你的配置看起来像这样:

<add key="SomethingsColor" value="Black" />

and you can convert it to a color:

你可以将它转换为颜色:

Color myColor = Color.FromName(ConfigurationManager.AppSettings["KEY"]);

#4


2  

Have a look at ColorTranslator. You'll be able to specify a color, say in appSettings and use ColorTranslator to convert it into a real color. In particular I've found the .FromHtml() method very useful.

看看ColorTranslator。您将能够指定颜色,例如在appSettings中,并使用ColorTranslator将其转换为真实颜色。特别是我发现.FromHtml()方法非常有用。

#5


0  

I wrote this article about custom config sections in ASP.NET... but the principal (and the code) is the same for the "app.config" (non web apps). But if that's overkill for you, then you could just convert the string as is mentioned by a few other people.

我写了这篇关于ASP.NET中自定义配置部分的文章......但是“app.config”(非网络应用程序)的主体(和代码)是相同的。但如果这对你来说太过分了,那么你可以像其他一些人提到的那样转换字符串。

#6


0  

You could just store the color as an int value, which can be serialised, and add a property of type color which uses toArgb and from argb to convert it.

您可以将颜色存储为可以序列化的int值,并添加使用toArgb的类型为color的属性,并使用argb进行转换。

e.g.

private ColorInt

public Color shapeColor
{
    get {
         return Color.FromArgb(ColorInt);
     }
      set 
    {
        ColorInt = value.toargb()
    }
}

#1


3  

One way would be to specify one of the KnownColor values as the config text and then use Color.FromName to create the Color object.

一种方法是将一个KnownColor值指定为配置文本,然后使用Color.FromName创建Color对象。

#2


2  

Color is an oddity; regular xml-serialization doesn't normally work - hence you often need to add your own code, perhaps via TypeConverter:

颜色很奇怪;常规的xml-serialization通常不起作用 - 因此您经常需要添加自己的代码,可能通过TypeConverter:

static void Main()
{

    Test(Color.Red);
    Test(Color.FromArgb(34,125,75));
}
static void Test(Color color)
{
    TypeConverter converter = TypeDescriptor.GetConverter(typeof(Color));
    string s = converter.ConvertToInvariantString(color);
    Console.WriteLine("String: " + s);
    Color c = (Color) converter.ConvertFromInvariantString(s);
    Console.WriteLine("Color: " + c);
    Console.WriteLine("Are equal: " + (c == color));
}

Outputs:

String: Red
Color: Color [Red]
Are equal: True
String: 34, 125, 75
Color: Color [A=255, R=34, G=125, B=75]
Are equal: True

#3


2  

You config would look like this:

你的配置看起来像这样:

<add key="SomethingsColor" value="Black" />

and you can convert it to a color:

你可以将它转换为颜色:

Color myColor = Color.FromName(ConfigurationManager.AppSettings["KEY"]);

#4


2  

Have a look at ColorTranslator. You'll be able to specify a color, say in appSettings and use ColorTranslator to convert it into a real color. In particular I've found the .FromHtml() method very useful.

看看ColorTranslator。您将能够指定颜色,例如在appSettings中,并使用ColorTranslator将其转换为真实颜色。特别是我发现.FromHtml()方法非常有用。

#5


0  

I wrote this article about custom config sections in ASP.NET... but the principal (and the code) is the same for the "app.config" (non web apps). But if that's overkill for you, then you could just convert the string as is mentioned by a few other people.

我写了这篇关于ASP.NET中自定义配置部分的文章......但是“app.config”(非网络应用程序)的主体(和代码)是相同的。但如果这对你来说太过分了,那么你可以像其他一些人提到的那样转换字符串。

#6


0  

You could just store the color as an int value, which can be serialised, and add a property of type color which uses toArgb and from argb to convert it.

您可以将颜色存储为可以序列化的int值,并添加使用toArgb的类型为color的属性,并使用argb进行转换。

e.g.

private ColorInt

public Color shapeColor
{
    get {
         return Color.FromArgb(ColorInt);
     }
      set 
    {
        ColorInt = value.toargb()
    }
}