简体   繁体   English

如何在C#中保存默认上下文设置?

[英]How to save default context settings in c#?

could somebody tell me why I can not save dynamic data in Settings.Default.Context ? 有人可以告诉我为什么我不能在Settings.Default.Context保存动态数据吗?

My code: 我的代码:

Settings.Default.Context.Add("myKey", "myValue");
Settings.Default.Save();

MessageBox.Show(Settings.Default.Context["myKey"].ToString());<-- This works

If I don't reload the appi everything works fine. 如果我不重新加载appi,一切正常。 But after reload application and calling only 但是在重新加载应用程序并仅调用之后

MessageBox.Show(Settings.Default.Context["myKey"].ToString());<-- error on appi reload

then I get an error like Object reference not set to an instance of an object. 然后我得到一个错误,例如Object reference not set to an instance of an object. . Why I can not save the context? 为什么我不能保存上下文? What's the problem? 有什么问题?

I'm using this way saving because of then I'm able to dynamically add new keys and values. 我之所以使用这种保存方式,是因为这样我就可以动态添加新的键和值。

I don't think you can add new settings this way, they're read only as they're resources of the program itself. 我认为您不能以这种方式添加新设置,因为它们只是程序本身的资源,因此只能被读取。

What you can do it make a setting that is a System.Collections.Specialized.StringCollection , then add items to it. 您可以做的是将其设置为System.Collections.Specialized.StringCollection ,然后向其中添加项目。 These extra items will still be there after the app closes. 应用关闭后,这些额外的物品仍将存在。

But you can't make a completely new setting and have it still be there. 但是,您无法进行全新的设置并将其保留在那里。

uhm what is the type of Settings? 设置的类型是什么? Anyway I think you missed .Context ..try to write: 无论如何,我认为您错过了.Context ..尝试编写:

Settings.Default.Context["myKey"].ToString()

The problem is that the value is an User-Scope setting and persists only for the duration of the application session. 问题在于该值是一个用户范围设置,仅在应用程序会话期间一直存在。 I think you need application-scope settings which can only be changed at design time ( in the Project Properties -> Settings tab ) or by altering the .exe.config file in between application sessions ( http://msdn.microsoft.com/en-us/library/bb397744.aspx ) 我认为您需要仅在设计时(在“项目属性”->“设置”选项卡中)或通过在应用程序会话之间更改.exe.config文件才能更改的应用程序作用域设置( http://msdn.microsoft.com/ zh-CN / library / bb397744.aspx

You have to do something like this: 您必须执行以下操作:

    using System.Configuration;

namespace WindowsFormsApplication1
{
  class MySettings : ApplicationSettingsBase
  {
    [UserScopedSetting]
    public string SavedString
    {
      get { return ( string )this["SavedString"]; }
      set { this["SavedString"] = value; }
    }
  } 

  public partial class Form1 : Form
  {
    MySettings m_Settings;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load( object sender, EventArgs e )
    {
      m_Settings = new MySettings();

      Binding b = new Binding( "Text", m_Settings, "SavedString", true, DataSourceUpdateMode.OnPropertyChanged );
      this.DataBindings.Add( b );
    }

    private void Form1_FormClosing( object sender, FormClosingEventArgs e )
    {
      m_Settings.Save();
    }

    private void button1_Click( object sender, EventArgs e )
    {
      this.Text = "My Text";
    }
  }
}

This application creates a Form with no caption and a button in the center. 此应用程序创建一个不带标题的表格,中间有一个按钮。 Once clicked on the button the .Text ( so the caption ) changes and it's saved when closing the form. 单击按钮后,.Text(即标题)将更改,并在关闭表单时将其保存。 Rerun the application and you will have it with the new caption My Text :) 重新运行该应用程序,您将获得新标题“我的文本” :)

If you need the complete source code just tell me :) 如果您需要完整的源代码,请告诉我:)

Viewed in object browser: 在对象浏览器中查看:

public override SettingsContext Context { get; 公共重写SettingsContext上下文{ } }

Read only? 只读?

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

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