简体   繁体   中英

Initializing custom class in settings file

In my WinForms C# Application I am attempting to save a custom class in a settings file. 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.

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. Any directions on this or other reasons for my null error would be appreciated

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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