简体   繁体   English

在设置文件中初始化自定义类

[英]Initializing custom class in settings file

In my WinForms C# Application I am attempting to save a custom class in a settings file. 在我的WinForms C#应用程序中,我试图将自定义类保存在设置文件中。 here is my current code: 这是我当前的代码:

public class PanelSaver : ApplicationSettingsBase
{
  private DockingStyle ds;
  System.Drawing.Point location;
  System.Drawing.Size panelSize;

  [UserScopedSetting()]
  [SettingsSerializeAs(System.Configuration.SettingsSerializeAs.Xml)]
  public DockingStyle Style
  {
     get { return ds; }
     set { ds = value; }
  }
  public System.Drawing.Point Location
  {
     get { return location; }
     set { location = value; }
  }
  public System.Drawing.Size Size
  {
     get { return panelSize; }
     set { panelSize = value; }
  }
}

The DockingStyle variable comes from DevExpress Dockpanels. DockingStyle变量来自DevExpress Dockpanels。

Here are the objects being used in the Settings file : 以下是“设置”文件中使用的对象:

在此处输入图片说明

When I attempt to use these variables, by passing them into this function: 当我尝试使用这些变量时,请将它们传递给此函数:

private void DockPanelSave(PanelSaver savingPanel, DockPanel realDockingPanel)//
{
   try
   {
      savingPanel.Location = realDockingPanel.Location;
      savingPanel.Size = realDockingPanel.Size;
      savingPanel.Style = realDockingPanel.Dock;
   }
   catch (Exception e)
   {
      MessageBox.Show(e.Message);
      throw new NotSupportedException("You may have added a new panel, and may not have added a corresponding " +
      "settings variable to hold it in the Settings.settings file");
   }

}

I get a null error. 我得到一个空错误。

How should I initialize the values in the settings file? 我应该如何初始化设置文件中的值? I am guessing I need to put in some kind of XML however I don't know the correct steps to take for generating it. 我猜想我需要放入某种XML,但是我不知道生成该XML的正确步骤。 Any directions on this or other reasons for my null error would be appreciated 关于此错误或其他原因导致我的null错误的任何指示,我们将不胜感激

You can see by this answer I show how to do what you are asking. 您可以从这个答案中看到我展示了如何去做您要问的事情。

It isn't the built in serialization, but you can define your class by my example and get the same result. 它不是内置的序列化,但是您可以通过我的示例定义类并获得相同的结果。

Here's another example , 这是另一个例子

and another 还有另一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM