.NET 应用程序设置:创建颜色值数组

发布于 2024-11-23 16:10:29 字数 1302 浏览 0 评论 0原文

通常,您可以通过在代码中手动更改数据类型来在应用程序设置中创建数组。然而,System.Drawing.Color 似乎是一个例外。您可以像往常一样将其添加到代码中,甚至可以像其他设置一样在图形编辑器中编辑值。不过,序列化数据似乎存在问题,因为它随后丢失了。

然后我在代码中寻找方法,认为编辑器在某个地方有错误,所以在设置文件中尝试了这个:

<Setting Name="ChannelColour" Type="System.Drawing.Color[]" Scope="User">
  <Value Profile="(Default)"><?xml version="1.0" encoding="utf-16"?>
    <ArrayOfColor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <color>Cyan</color>
      <color>DarkOrange</color>
      <color>Magenta</color>
      <color>LawnGreen</color>
    </ArrayOfColor>
  </Value>
</Setting>

同样在 app.config 中:

<setting name="ChannelColour" serializeAs="Xml">
  <value>
    <ArrayOfColor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <color>Cyan</color>
      <color>DarkOrange</color>
      <color>Magenta</color>
      <color>LawnGreen</color>
    </ArrayOfColor>
  </value>
</setting>

然后我没有触摸编辑器并确保任何现有的配置文件在哪里走了。还是没用。

为什么其他数组工作正常,并且颜色值本身是默认选项之一,但组合根本不起作用?

Usually you can make an array in the application settings by changing the data type manually in the code. However, System.Drawing.Color seems to be an exception. You can add it in the code as usually, and it even lets you edit the values in the graphical editor like other settings. It seems there is a problem serializing the data though, as it is subsequently lost.

I then looked for methods in the code thinking the editor had got a bug in it somewhere, so tried this in the settings file:

<Setting Name="ChannelColour" Type="System.Drawing.Color[]" Scope="User">
  <Value Profile="(Default)"><?xml version="1.0" encoding="utf-16"?>
    <ArrayOfColor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <color>Cyan</color>
      <color>DarkOrange</color>
      <color>Magenta</color>
      <color>LawnGreen</color>
    </ArrayOfColor>
  </Value>
</Setting>

And likewise in app.config:

<setting name="ChannelColour" serializeAs="Xml">
  <value>
    <ArrayOfColor xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <color>Cyan</color>
      <color>DarkOrange</color>
      <color>Magenta</color>
      <color>LawnGreen</color>
    </ArrayOfColor>
  </value>
</setting>

I then didn't touch the editor and made sure any existing config files where gone. It still didn't work.

Why is it that other arrays work fine, and Color values on their own are one of the default options, yet the combination doesn't work at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-11-30 16:10:29

最后我这样做了:

首先,我添加了一个名为 ColorCollection 的类,它基本上是 Color[] 的包装器,只有一个主要区别(事件处理)。

向 ColorCollection 的 Settings 类添加了一个属性,然后将设置存储为 StringCollection,我使用在 ColorCollection (ToString) 中添加/重载的方法,使用设置加载和保存事件处理程序手动设置该设置解析)。

由于更改和数组下标不会触发 PropertyChanged 事件,因此我添加了一个名为 Changed 的新事件,该事件可以由其他类调用 (PropertyChanged 因此即使在 Settings 类中我们也无法手动调用它,然后我将事件触发器添加到 set 语句中。

> 是在另一个类中定义的, 至少有一定的水平抽象的,所以混乱不会立即可见,我最终对设置中需要的大多数数组做了类似的事情,因为我需要在下标更改时触发事件。

In the end I did it like this:

First I added a class called ColorCollection which was basically a wrapper around Color[] with only one main difference (event handling).

Added a property to the Settings class for a ColorCollection and then stored the settings as a StringCollection that I manually set using the Settings loading and saving event handlers using methods I added/overloaded in ColorCollection (ToString and Parse).

Because changing and array subscript doesn't trigger the PropertyChanged event, I then added a new event called Changed that could be invoked by other classes (PropertyChanged is defined in another class so we can't manually invoke it even from within the Settings class. I then added the event trigger to the set statement.

It's messy, but at least there is some level of abstraction so the messiness isn't instantly visible. I ended up doing something similar for most arrays I needed in settings as I needed the events to be triggered if a subscript changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文