65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 XML 管理应用程序状态数据

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.67/5 (3投票s)

2006年9月26日

2分钟阅读

viewsIcon

23612

downloadIcon

254

一个实用工具,用于通过 XML 捕获/恢复容器中的状态数据。

Sample Image

引言

以下是一个实用工具,它遍历容器中的控件,并将控件的值保存或恢复到 XML 文件中。 封闭的命名空间捕获应用程序数据,也称为粘性数据或状态数据。 该概念与现在已被弃用的 INI 文件或 Windows 注册表相同。 在我准备发布这篇文章时,我偶然发现 Chad Z. Hower,又名 Kudzu 的一篇投稿。 Chad 的文章使用不同的方法完成了相同的任务,我鼓励您阅读它。 我呈现给您的过程与 Chad 的容器类耦合度较低,但在处理非文本数据方面功能较弱。

使用代码

演示项目包含三个容器,一个窗体和一个带有两个页面的选项卡控件。 每个容器在可执行文件所在的同一目录中创建自己的 XML 文件。 要使用此代码,请在您的项目中包含命名空间 PFM_FormStateDataPFM_Wrap_Ctrl_Value_Property。 有两种方法可以保存状态数据,一种方法可以恢复状态数据

  1. 下面显示的第一种方法最易于使用,因为它适用于容器中的所有(类似文本的)控件。 随着向容器添加新控件,它们默认情况下会成为解决方案的一部分。
  2. 第二种方法允许程序员进行更大的控制,但实现起来需要更多的努力。 第二种方法要求程序员明确指定哪些控件是解决方案的一部分。
  3. 最后,代码的第三部分显示了如何将 XML 文件内容恢复到容器上的控件中。
// all code taken form the demo project form.cs
//Method 1 of 2. 
//SAVE defaults from FORM for use next 
//time the application is run.
Control.ControlCollection allControlHeldByThisContainer = this.Controls;
//all controls on this form are candidates for save _
//except those enrolled in 'exceptionArray'.

//a list of controls not to save defaults for, could be null
Control[] exceptionArray = new Control[2];
//but for this example, we will not save radio1 and radio3
exceptionArray[0] = radioButton1;
exceptionArray[1] = radioButton3;

//used to give each container its own file on disk. 
//The container name makes a good default.
string sContainerName = this.Name.ToString();
//a comment to help document the resulting xml file. 
//The container title makes a good default.
string sTitle = this.Text;
SaveRestoreControlDefaults frmDfts = 
    new SaveRestoreControlDefaults(
                      SaveRestoreControlDefaults.eIO.save,
                      allControlHeldByThisContainer,
                      exceptionArray,
                      sContainerName,
                      sTitle);
                      
//------------------------------------------------------------------

//Method 2 of 2.
//In the prior example, the assumption was that 
//defaults where to be saved for most or all controls.
//In this example defaults will only be saved 
//for an explicit few controls.
//In method 1 of 2 there is an assumed 
//relationship between labels and controls.
//In this method, an explicit list 
//of label[0] / control[1] must be setup by you the programmer.
//SAVE default for just ONE control on 
//TAB PAGE 2 for use next time the application is run.
Control[] desiredCtrls = new Control[2];
//always include the controls label
desiredCtrls[0] = this.lblCheckListBox;
desiredCtrls[1] = this.checkedListBox1;
//...

//name of this container to be used in xml file name
string sContainerName = this.tabPage2.Name.ToString();
//a comment to help document the resulting xml file.
string sTitle = "Controls from tabpage2";
SaveRestoreControlDefaults tabDfts = new SaveRestoreControlDefaults();
tabDfts.ExplicitySaveControls(desiredCtrls, sContainerName, sTitle);

//--------------------------------------------------------------------
//RESTORE defaults to the FORM from the last time application was run.
//Loading controls is always done the same way. 
//Loading does not have options like saving.
SaveRestoreControlDefaults frmDfts = new SaveRestoreControlDefaults(
     SaveRestoreControlDefaults.eIO.load, //loading defaults flag
     this.Controls, //controls on a container form
     null, //a list of controls to ignore - in this example none.
     this.Name.ToString(), //name of this container used 
                           //when the xml file was created.
     null); //a comment added to the top node 
            //of the xml file - only used when being saved

关注点

就是这样了。 将信息捕获和恢复到您的容器的过程就像我能想象到的那样松散耦合。 我必须告诉你,我只是偶尔才使用 C#,但我有很多 C++ 经验,我认为这是一个值得分享的实用工具。

最后,此代码的任何部分都不是抄袭的,您可以按原样使用或修改它,没有任何限制。 甚至可以把你的名字写在上面。

历史

VB.NET 源代码目前不可用。

© . All rights reserved.