By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Saving color with ApplicationSettingsBase

P: n/a
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.UserScopedSettingAttr ibute()]
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
Jan 8 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
fbolefeysot wrote:
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.UserScopedSettingAttr ibute()]
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.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Jan 8 '08 #2

P: n/a
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" wrote:
fbolefeysot wrote:
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.UserScopedSettingAttr ibute()]
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.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Jan 8 '08 #3

P: n/a
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" wrote:
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" wrote:
fbolefeysot wrote:
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.UserScopedSettingAttr ibute()]
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.

--
Lasse Vågsæther Karlsen
mailto:la***@vkarlsen.no
http://presentationmode.blogspot.com/
Jan 8 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.