【问题标题】:Initializing custom class in settings file在设置文件中初始化自定义类
【发布时间】:2012-06-26 09:17:06
【问题描述】:

在我的 WinForms C# 应用程序中,我试图将自定义类保存在设置文件中。这是我当前的代码:

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; }
  }
}

DockingStyle 变量来自 DevExpress Dockpanels。

以下是设置文件中使用的对象:

当我尝试使用这些变量时,将它们传递给这个函数:

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");
   }

}

我得到一个空错误。

我应该如何初始化设置文件中的值?我猜我需要输入某种 XML,但是我不知道生成它的正确步骤。对于我的空错误的这个或其他原因的任何指示将不胜感激

【问题讨论】:

    标签: c# serialization devexpress settings dockpanel


    【解决方案1】:

    您可以通过this answer 看到我展示了如何按照您的要求进行操作。

    它不是内置的序列化,但您可以通过我的示例定义您的类并获得相同的结果。

    Here's another example,

    and another

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2011-08-28
      • 1970-01-01
      • 2020-09-26
      相关资源
      最近更新 更多