首页 > 解决方案 > 如何创建结构列表类型的应用程序设置参数?

问题描述

在我的项目中,我有一个自定义结构:

struct Point {
  public uint xPoint { get; }
  public uint yPoint { get; }

  public Point(uint x, uint y) {
    xPoint = x;
    yPoint = y;
  }
}

我正在使用这些点的列表:

List<Point> pathToNavigate = new List<Point>();

我想要做的是将我的点列表保存到 Settings.settings: 像这样

我不知道如何将字符串更改为我的结构点列表。

我尝试弄乱 xml 并手动添加我的选项,但我不知道该怎么做。我发现的大多数事情都告诉我使用自定义命名空间,但我也无法使用我的 Point 结构列表来使用它。

编辑:我的问题是使用列表的自定义结构。问题不在于将项目添加到列表中,而是能够正确加载它们的内容。

标签: c#listvisual-studiostructapplication-settings

解决方案


将副本应用于您的案例(我无法标记它,因为我无法撤销关闭,我发布此答案以解决您的困难,并且比各种教程和副本更精确和更完整):

如何在 Settings.Default 上保存 List<string>?

例如,在下面的命名空间中具有可序列化的结构:

namespace WindowsFormsAppTest
{
  [Serializable]
  public struct Point
  {
    public uint xPoint { get; set; }
    public uint yPoint { get; set; }

    public Point(uint x, uint y)
    {
      xPoint = x;
      yPoint = y;
    }
  }
}

如前所述,属性必须是可读写的,所以我添加了自动设置器,并且结构必须通过添加属性来序列化。

编译项目。

您需要创建一个字符串参数,例如具有名称MyList

在此处输入图像描述

然后Settings.settings使用任何文本编辑器手动编辑,将其类型更改为:

System.Collections.Generic<WindowsFormsAppTest.Point>

在 HTML 文本编码中,例如:

System.Collections.Generic.List&lt;WindowsFormsAppTest.Point&gt;

设置.settings

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsFormsAppTest.Properties" GeneratedClassName="Settings">
  <Profiles />
  <Settings>
    <Setting Name="MyList" Type="System.Collections.Generic.List&lt;WindowsFormsAppTest.Point&gt;" Scope="User">
      <Value Profile="(Default)" />
    </Setting>
  </Settings>
</SettingsFile>

保存后,进入 Visual Studio 重新加载文件,就可以看到类型改变了:

在此处输入图像描述

您需要使用设计器通过扩展参数的类型并单击此自定义列表类型来更新此设置生成的 C# 代码文件:

在此处输入图像描述

如果没有,或者出现问题,您需要手动更新这些文件:

app.config 到 serializeAs Xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsFormsAppTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup>
    <userSettings>
        <WindowsFormsAppTest.Properties.Settings>
            <setting name="MyList" serializeAs="Xml">
                <value />
            </setting>
        </WindowsFormsAppTest.Properties.Settings>
    </userSettings>
</configuration>

Settings.Designer.cs 更改属性的类型

public global::System.Collections.Generic.List<WindowsFormsAppTest.Point> MyList {
    get {
        return ((global::System.Collections.Generic.List<WindowsFormsAppTest.Point>)(this["MyList"]));
        }

全部保存和/或再次编译。

Main现在,您可以在方法或主窗体的构造函数或加载事件处理程序中编写示例:

private void FormTest_Load(object sender, EventArgs e)
{
  if ( Properties.Settings.Default.MyList == null )
    Properties.Settings.Default.MyList = new List<Point>();
  Properties.Settings.Default.Save();
}

例如在按钮单击事件处理程序中:

private void ButtonCreate_Click(object sender, EventArgs e)
{
  Properties.Settings.Default.MyList.Add(new Point(10, 10));
  Properties.Settings.Default.MyList.Add(new Point(10, 20));
  Properties.Settings.Default.MyList.Add(new Point(20, 20));
  Properties.Settings.Default.MyList.Add(new Point(50, 50));
  Properties.Settings.Default.Save();
}

现在配置文件是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <WindowsFormsAppTest.Properties.Settings>
            <setting name="MyList" serializeAs="Xml">
                <value>
                    <ArrayOfPoint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                        <Point>
                            <xPoint>10</xPoint>
                            <yPoint>10</yPoint>
                        </Point>
                        <Point>
                            <xPoint>10</xPoint>
                            <yPoint>20</yPoint>
                        </Point>
                        <Point>
                            <xPoint>20</xPoint>
                            <yPoint>20</yPoint>
                        </Point>
                        <Point>
                            <xPoint>50</xPoint>
                            <yPoint>50</yPoint>
                        </Point>
                    </ArrayOfPoint>
                </value>
            </setting>
        </WindowsFormsAppTest.Properties.Settings>
    </userSettings>
</configuration>

要更改一个项目,我们可以编写,因为作为一个结构体所以是一个值类型:

private void ButtonUpdate_Click(object sender, EventArgs e)
{
  var point = Properties.Settings.Default.MyList[0];
  point.xPoint = 100;
  point.yPoint = 100;
  Properties.Settings.Default.MyList[0] = point;
  Properties.Settings.Default.Save();
}

现在设置文件有:

<Point>
    <xPoint>100</xPoint>
    <yPoint>100</yPoint>
</Point>
...

为了测试这一点,我们可以使用另一个按钮列出项目:

private void ButtonShow_Click(object sender, EventArgs e)
{
  var list = Properties.Settings.Default.MyList.Select(p => $"{p.xPoint}, {p.yPoint}");
  MessageBox.Show(string.Join(Environment.NewLine, list.ToArray()));
}

在此处输入图像描述

笔记

要清理应用程序设置,您需要删除所有这些文件夹:

c:\Users\User\AppData\Local\Organization\WindowsFormsAppTest.exe_Url_*

whereOrganizationWindowsFormsAppTest来自AssemblyInfo.csusing fieldsAssemblyCompanyAssemblyTitle.


推荐阅读