首页 > 解决方案 > The method or operation is not implemented exception when trying to use custom class for user setting

问题描述

I'm trying to save window's position as a user setting so I can restore it when my app starts up. I add a setting using the designer of this type:

using System.Configuration;

namespace MyApp.Framework
{
   [SettingsSerializeAs(SettingsSerializeAs.Xml)]
   public class SavedWindowSettings
   {
      public double Left { get; set;}
      public double Top { get; set;}

      public SavedWindowSettings()
      { }

      public SavedWindowSettings(double left, double top)
      {
        Left = left;
        Top = top;
      }
   }
}

It compiles, but when I run I get System.NotImplementedException: 'The method or operation is not implemented.' on the application, but the last thing in the call stack is PresentationFramework.dll!System.Windows.Baml2006.Baml2006SchemaContext.ResolveBamlType(System.Windows.Baml2006.Baml2006SchemaContext.BamlType bamlType, short typeId) Unknown, which doesn't help much.

I think is has to do with App.config. If I change the type of setting to string, my App.config has a

 <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <section name="MyApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
 </sectionGroup>

and

 <userSettings>
   <MyApp.Properties.Settings>
      <setting name="MainWindowPlacement" serializeAs="String">
         <value />
      </setting>
   </MyApp.Properties.Settings>
 </userSettings>

sections. I tried adding them back in, changing serializeAs to "xml", with the same exception. What am I missing?

标签: c#

解决方案


The first thing that screams out is that you have no setter's defined for Left and Top. Why don't you have:

public double Left { get; set; }
public double Top { get; set; }

The exception could be from the runtime reporting that there is no method for setting your props.


推荐阅读