如何获得控件的属性,然后将其保存为XML并加载回来?将其、控件、保存为、如何获得

2023-09-07 00:45:12 作者:旧念碎影丶独留我一袭烟岚

其实,我需要4 methodes。我使用C#.NET 3.5和Windows Forms。

Actually I would need 4 methodes. I'm using c# .NET 3.5 and windows forms.

在得到所有控制特性(也子性质在喜欢的MenuItems等),从目前的形式,其中名称匹配列表名称//不工作正常 将结果保存到XML //不知道如何保存结果 从XML负载的结果, 从XML最后设置加载控件属性。 //工作的伟大

现在我在做这种形式的第1步:

Now I'm doing this form step 1:

  public static Dictionary<string, object> xmlDictionary;
  public Control FindControlRecursive(Control container, List<string> properties)
  {
     foreach (Control controls in container.Controls)
     {
        Type controlType = controls.GetType();
        PropertyInfo[] propertyInfos = controlType.GetProperties();
        foreach (PropertyInfo controlProperty in propertyInfos)
        {
           foreach (string propertyName in properties)
           {
              if (controlProperty.Name == propertyName)
              {
                 xmlDictionary.Add(controlProperty.Name, controlProperty.GetValue(controls, null));
              }
           }
        }
        Control foundCtrl = FindControlRecursive(controls, properties);
        if (foundCtrl != null)
           return foundCtrl;

     }
     return null;
  }

调用梅托德:

Calling the metod:

     List<string> propertyNames = new List<string>(); //list of all property names I want to save

     propertyNames.Add("Name");
     propertyNames.Add("Text");
     propertyNames.Add("Size");

     FindControlRecursive(this, propertyNames); //this is the current form

这个方法不会返回所有的控制性能,我不知道为什么。

This method doesn't return all control properties and I dont know why.

第四步:

//field = some new field
//newValue = new value
    FieldInfo controlField = form.GetType().GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    object control = controlField.GetValue(form);
    PropertyInfo property = control.GetType().GetProperty(newValue);
    property.SetValue(control, items.Value, new object[0]);

步骤4的工作很好,但不知道如何遍历XML reults。

Step 4 work great, but don't know how to iterate through XML reults.

能否请你帮我解决这些问题。

Could you please help me to solve these problems.

感谢和问候。

推荐答案

您知道,Windows窗体有一个现有设置的基础设施,你可以用它来保存控件和表单设置?从设计,选择一个控制和应用程序设置下的属性,然后属性绑定,可以绑定任何财产的控制,将生成访问和保存属性值的属性。该设置可以是应用程序或用户的范围。这些设置也将使用独立存储,让您升级到不同版本的设置,维护版本,和许多其他功能的用户设置。因此,这可能不会直接回答你的问题,但可能对您的特定问题更好的解决方案。一旦绑定属性,您可以将更改保存,只要你想,在每个用户或每个应用程序的基础,像这样:

Are you aware that with Windows Forms there is an existing settings infrastructure you can use to save settings of controls and your forms? From the designer, select a control and in the properties under Application Settings, then Property Binding, you can bind any property on the control to a property that will be generated to access and save that property value. The settings can be application or user scoped. These settings will also use isolated storage, allow you to upgrade to different versions of settings to maintain user settings between versions, and many other features. So this may not directly answer your question, but may be a better solution for your particular problem. Once you bind a property you can save changes whenever you want, on a per user or per application basis like so:

Settings.Default.TextBox1 = textBox2.Text; Settings.Default.BackColor = Color.Blue; Settings.Default.Save();

Settings.Default.TextBox1 = textBox2.Text; Settings.Default.BackColor = Color.Blue; Settings.Default.Save();