Saving color with ApplicationSettingsBase

F

fbolefeysot

I have a 'Filter' class that contains some strings, 1 bool and 2 Color
attributes.
I made an array of Filter : Filter[], and I'd like to save this array with
the application settings system.
here is my ApplicationSettingsBase-derived class :
[global::System.Configuration.UserScopedSettingAttribute()]
public Filter[] filtersList
{
get{return ((Filter[])(this["filtersList"])); }
set{ this["filtersList"] = value; }
}
When I call the Save method on this class, it does save my array of Filter
in my user settings, under an XML form, but each saved Filter always have
empty color values, while other Filter strings are ok.

here is a copy of the xml-saved user settings with 1 element in the array :

<setting name="filtersList" serializeAs="Xml">
<value>
<ArrayOfFilter xmlns:xsi="http://www.w3.org/.... ... .."
xmlns:xsd="http://www.w3.org/2001/ .. .. ..">
<Filter>
<name>toto</name>
<pattern>
<string>valeur : 11</string>
</pattern>
<backColor />
<foreColor />
<enabled>true</enabled>
</Filter>
</ArrayOfFilter>
</value>
</setting>


How can I have my custom class completely saved ?
thanks
 
L

Lasse Vågsæther Karlsen

fbolefeysot said:
I have a 'Filter' class that contains some strings, 1 bool and 2 Color
attributes.
I made an array of Filter : Filter[], and I'd like to save this array with
the application settings system.
here is my ApplicationSettingsBase-derived class :
[global::System.Configuration.UserScopedSettingAttribute()]
public Filter[] filtersList
{
get{return ((Filter[])(this["filtersList"])); }
set{ this["filtersList"] = value; }
}
When I call the Save method on this class, it does save my array of Filter
in my user settings, under an XML form, but each saved Filter always have
empty color values, while other Filter strings are ok.

here is a copy of the xml-saved user settings with 1 element in the array :

<setting name="filtersList" serializeAs="Xml">
<value>
<ArrayOfFilter xmlns:xsi="http://www.w3.org/.... ... .."
xmlns:xsd="http://www.w3.org/2001/ .. .. ..">
<Filter>
<name>toto</name>
<pattern>
<string>valeur : 11</string>
</pattern>
<backColor />
<foreColor />
<enabled>true</enabled>
</Filter>
</ArrayOfFilter>
</value>
</setting>


How can I have my custom class completely saved ?
thanks

First, add [XmlIgnore] to the color properties, this will stop the
properties from being output empty in the xml file.

Then, add new properties like this:

/// <summary>
/// This is a shadow property to make sure <see
cref="PauseBackgroundColor"/>
/// is serialized.
/// </summary>
[XmlElement("color_fadeout", Type = typeof(Int32))]
public Int32 ShadowFadeoutColor
{
get
{
return _FadeoutColor.ToArgb();
}

set
{
_FadeoutColor = Color.FromArgb(value);
}
}

This is from my own class, replace the names as appropriate. This will
serialize the colors as Int32 values. I'm sure Jon Skeet has a better
way to do this :) but this worked for me.
 
F

fbolefeysot

of course, I can tweak my color property so that it spits int or strings, and
to the translation myself.
But Color are supposed to be stored without any trick.
I tried to add a new color property along with my array, and initialized it
with specific values. it got saved along with the array, and with right
values :

<setting name="testColor" serializeAs="String">
<value>10, 20, 30</value>
</setting>

So why do colors inside my class do not store their values correctly, while
a color outside the class does ?
Can I do something so that colors inside my class is stored correctly ?

Lasse Vågsæther Karlsen said:
fbolefeysot said:
I have a 'Filter' class that contains some strings, 1 bool and 2 Color
attributes.
I made an array of Filter : Filter[], and I'd like to save this array with
the application settings system.
here is my ApplicationSettingsBase-derived class :
[global::System.Configuration.UserScopedSettingAttribute()]
public Filter[] filtersList
{
get{return ((Filter[])(this["filtersList"])); }
set{ this["filtersList"] = value; }
}
When I call the Save method on this class, it does save my array of Filter
in my user settings, under an XML form, but each saved Filter always have
empty color values, while other Filter strings are ok.

here is a copy of the xml-saved user settings with 1 element in the array :

<setting name="filtersList" serializeAs="Xml">
<value>
<ArrayOfFilter xmlns:xsi="http://www.w3.org/.... ... .."
xmlns:xsd="http://www.w3.org/2001/ .. .. ..">
<Filter>
<name>toto</name>
<pattern>
<string>valeur : 11</string>
</pattern>
<backColor />
<foreColor />
<enabled>true</enabled>
</Filter>
</ArrayOfFilter>
</value>
</setting>


How can I have my custom class completely saved ?
thanks

First, add [XmlIgnore] to the color properties, this will stop the
properties from being output empty in the xml file.

Then, add new properties like this:

/// <summary>
/// This is a shadow property to make sure <see
cref="PauseBackgroundColor"/>
/// is serialized.
/// </summary>
[XmlElement("color_fadeout", Type = typeof(Int32))]
public Int32 ShadowFadeoutColor
{
get
{
return _FadeoutColor.ToArgb();
}

set
{
_FadeoutColor = Color.FromArgb(value);
}
}

This is from my own class, replace the names as appropriate. This will
serialize the colors as Int32 values. I'm sure Jon Skeet has a better
way to do this :) but this worked for me.
 
F

fbolefeysot

As I'm curious, I did some more tries :

a single instance of my Filter Class does not store its color correctly...
an array of Colors does not store its colors correctly...

so it seems that as long as the 'Color' class is inside a struct, class or
Array, it is not saved any more by the settings system.

How can I fix that ?

fbolefeysot said:
of course, I can tweak my color property so that it spits int or strings, and
to the translation myself.
But Color are supposed to be stored without any trick.
I tried to add a new color property along with my array, and initialized it
with specific values. it got saved along with the array, and with right
values :

<setting name="testColor" serializeAs="String">
<value>10, 20, 30</value>
</setting>

So why do colors inside my class do not store their values correctly, while
a color outside the class does ?
Can I do something so that colors inside my class is stored correctly ?

Lasse Vågsæther Karlsen said:
fbolefeysot said:
I have a 'Filter' class that contains some strings, 1 bool and 2 Color
attributes.
I made an array of Filter : Filter[], and I'd like to save this array with
the application settings system.
here is my ApplicationSettingsBase-derived class :
[global::System.Configuration.UserScopedSettingAttribute()]
public Filter[] filtersList
{
get{return ((Filter[])(this["filtersList"])); }
set{ this["filtersList"] = value; }
}
When I call the Save method on this class, it does save my array of Filter
in my user settings, under an XML form, but each saved Filter always have
empty color values, while other Filter strings are ok.

here is a copy of the xml-saved user settings with 1 element in the array :

<setting name="filtersList" serializeAs="Xml">
<value>
<ArrayOfFilter xmlns:xsi="http://www.w3.org/.... ... .."
xmlns:xsd="http://www.w3.org/2001/ .. .. ..">
<Filter>
<name>toto</name>
<pattern>
<string>valeur : 11</string>
</pattern>
<backColor />
<foreColor />
<enabled>true</enabled>
</Filter>
</ArrayOfFilter>
</value>
</setting>


How can I have my custom class completely saved ?
thanks

First, add [XmlIgnore] to the color properties, this will stop the
properties from being output empty in the xml file.

Then, add new properties like this:

/// <summary>
/// This is a shadow property to make sure <see
cref="PauseBackgroundColor"/>
/// is serialized.
/// </summary>
[XmlElement("color_fadeout", Type = typeof(Int32))]
public Int32 ShadowFadeoutColor
{
get
{
return _FadeoutColor.ToArgb();
}

set
{
_FadeoutColor = Color.FromArgb(value);
}
}

This is from my own class, replace the names as appropriate. This will
serialize the colors as Int32 values. I'm sure Jon Skeet has a better
way to do this :) but this worked for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top