如何创建结构列表类型的应用程序设置参数? [英] How to create an application settings parameter of type a list of structs?

查看:48
本文介绍了如何创建结构列表类型的应用程序设置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

struct Point {公共uint xPoint {}公共uint yPoint {get;}公共点(uint x,uint y){xPoint = x;yPoint = y;}} 

我正在使用以下要点列表:

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

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

我不知道如何将字符串更改为struct Point的列表.

我尝试弄乱xml并手动添加我的选项,但是我不知道该怎么做.我发现的大多数事情都告诉我要使用自定义名称空间,但使用Point struct列表也无法实现该功能.

我的问题是使用列表的自定义结构.问题不是将项目添加到列表中,而是能够正确加载其内容.

解决方案

以下步骤有效.

但是,我认为您的问题仅仅是因为 xPoint yPoint 没有公共设置程序.这是由于 XmlSerializer 作品.请在此处a>.

首先,创建一个设置.在这种情况下,我将其命名为 ListOfPoints .类型无关紧要,我们还是要更改它.

手动编辑"Settings.settings".我只是使用Visual Studio的XML编辑器打开它,但是使用您喜欢的东西.

然后仅更改设置的类型.请注意,您需要对< > 使用HTML编码.

整个设置.设置:

 <?xml version ='1.0'encoding ='utf-8'?>< SettingsFile xmlns ="http://schemas.microsoft.com/VisualStudio/2004/01/settings"CurrentProfile =(默认)"GeneratedClassNamespace ="WindowsFormsApp1.Properties";GeneratedClassName =设置"><个人资料/><设置><设置名称="ListOfPoints"类型="System.Collections.Generic.List& lt; WindowsFormsApp1.MyPoint& gt;".范围=用户".< Value Profile =(默认)"";/></设置></设置></SettingsFile> 

唯一的更改是:

  Type ="System.Collections.Generic.List< WindowsFormsApp1.MyPoint& gt;" 

所有代码:

  [可序列化]公共结构MyPoint{公共uint X {get;放;}公共单位Y {放;}公共MyPoint(uint x,uint y){X = x;Y = y;}公共重写bool等于(object obj){如果(!(obj是MyPoint))返回false;var other =(MyPoint)obj;返回other.X == X&&Y == Y;}公共重写int GetHashCode(){返回未经检查的(X.GetHashCode()^ Y.GetHashCode());}}私有静态只读List< MyPoint>saveMe = new List< MyPoint>();私有静态List< MyPoint>loadMe;私有静态void SaveData(){Properties.Settings.Default.ListOfPoints = saveMe;Properties.Settings.Default.Save();}私有静态void LoadData(){Properties.Settings.Default.Reload();loadMe = Properties.Settings.Default.ListOfPoints;测试数据();}私有静态无效TestData(){如果(loadMe.Count!= saveMe.Count)抛出新的异常(不同计数");对于(int i = 0; i< loadMe.Count; i ++){如果(!loadMe [i].等于(saveMe [i]))抛出新的异常(在索引{i}处不匹配"$" {nameof(MyPoint)});}} 

通过将任意内容添加到 saveMe 进行测试.然后运行 SaveData ,然后运行 LoadData .

如果数据不匹配,

LoadData 将引发异常.

In my project I have a custom struct:

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

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

I'm using a list of these Points:

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

What I'm trying to do is save a list of my Points to the Settings.settings:

I can't figure out how to change string to be a list of my struct Point.

I tried messing with the xml and manually add in my option but I can't work out how to do it. Most things I find tell me to use a custom namespace but I also can't get that working with a list of my Point struct.

Edit: My problem is with a custom struct using a list. The issue isn't adding the items to the list, it's being able to load their contents properly.

解决方案

The steps below work.

However, I believe your issue is simply because xPoint and yPoint do not have public setters. This is due to how XmlSerializer works. See the documentation here.

First, create a setting. In this case I named it ListOfPoints. The type is irrelevant, we're going to change it anyways.

Manually edit "Settings.settings". I just open it with Visual Studio's XML editor but use what you prefer.

Then change the type of the setting only. Note you need to use HTML encoding for < and >.

Entire Settings.settings:

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

The only change made was this:

Type="System.Collections.Generic.List&lt;WindowsFormsApp1.MyPoint&gt;"

All Code:

[Serializable]
public struct MyPoint
{
    public uint X { get; set; }
    public uint Y { get; set; }

    public MyPoint(uint x, uint y)
    {
        X = x;
        Y = y;
    }

    public override bool Equals(object obj)
    {
        if (!(obj is MyPoint))
            return false;
        var other = (MyPoint)obj;
        return other.X == X && other.Y == Y;
    }

    public override int GetHashCode()
    {
        return unchecked(X.GetHashCode() ^ Y.GetHashCode());
    }
}

private static readonly List<MyPoint> saveMe = new List<MyPoint>();
private static List<MyPoint> loadMe;

private static void SaveData()
{
    Properties.Settings.Default.ListOfPoints = saveMe;
    Properties.Settings.Default.Save();
}

private static void LoadData()
{
    Properties.Settings.Default.Reload();
    loadMe = Properties.Settings.Default.ListOfPoints;
    TestData();
}

private static void TestData()
{
    if (loadMe.Count != saveMe.Count)
        throw new Exception("Different counts");
    for (int i = 0; i < loadMe.Count; i++)
    {
        if (!loadMe[i].Equals(saveMe[i]))
            throw new Exception($"{nameof(MyPoint)} at index {i} doesn't match");
    }
}

Test this by adding whatever you wish to saveMe. Then run SaveData followed by LoadData.

LoadData will throw an exception if the data doesn't match.

这篇关于如何创建结构列表类型的应用程序设置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆