.Net Micro Framework不支持文件系统(目前该项功能正在研发之中),所以无法像Windows和windows ce平台那样把需要永久保存的数据保存到文件之中。内存中保存的数据只要系统一掉电,所有的数据也都消失了,这对一些需要保存参数的应用来说真是不妙。
这几天在研究MF触摸屏功能时就遇到该问题,因为触摸屏校准之后,需要保存校准后的参数,否则MF一重启,难道还需要重新校准不成?
 感谢Donald Thompson 和 Rob S. Miles,从他们的大作上找到了问题的解决办法。办法就是把对象保存到Flash(EEPROM)中(有点像对象的二进制序列化)。
下面是我整理的示例代码(实现比较简单,但总觉得不太正规,不知道能存多大,也搞不清楚数据到底存放在什么位置了。):
 

  1. using System;  
  2. using Microsoft.SPOT;  
  3. using System.Collections;  
  4. using System.Threading;  
  5.    
  6. namespace DataStorage  
  7. {  
  8.     public class Program  
  9.     {         
  10.         public static void Main()  
  11.         {  
  12.             FlashDatas fd = FlashDatas.Load();  
  13.             fd.dump();  
  14.             fd.Flag = "adfg";  
  15.             //fd.Items.Clear();                  
  16.             //fd.AddItem(new FlashData(55, "1aaa"));  
  17.             //fd.AddItem(new FlashData(66, "2bbb"));  
  18.             //fd.AddItem(new FlashData(77, "3ccc"));  
  19.             //fd.AddItem(new FlashData(88, "4ddd"));  
  20.             fd.Save();  
  21.             Thread.Sleep(3000);               
  22.         }  
  23.    
  24.         [Serializable]  
  25.         private class FlashData  
  26.         {  
  27.             public DateTime date;  
  28.             public int iData;  
  29.             public string sData;  
  30.             public FlashData(int iData, string sData)  
  31.             {  
  32.                 date = DateTime.Now;  
  33.                 this.iData = iData;  
  34.                 this.sData = sData;  
  35.             }  
  36.    
  37.             public override string ToString()  
  38.             {  
  39.                 return date.ToString() + " " + iData.ToString() + " " + sData;   
  40.             }  
  41.         }  
  42.    
  43.         [Serializable]  
  44.         private class FlashDatas  
  45.         {  
  46.             public DateTime CreateTime = DateTime.Now;  
  47.             public string Flag = @"http://yfsoft.blog.51cto.com/";  
  48.             public ArrayList Items = new ArrayList();  
  49.    
  50.             public void dump()  
  51.             {  
  52.                 Debug.Print(CreateTime.ToString());  
  53.                 Debug.Print(Flag);   
  54.                 foreach (FlashData Item in Items)  
  55.                 {  
  56.                     if (Item != null)  
  57.                     {  
  58.                         Debug.Print(Item.ToString());  
  59.                     }  
  60.                 }  
  61.             }  
  62.             public void AddItem(FlashData Item)  
  63.             {  
  64.                 Items.Add(Item);  
  65.             }  
  66.    
  67.             //调入数据  
  68.             public static FlashDatas Load()  
  69.             {  
  70.                 ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(  
  71.                         typeof(FlashDatas),                       //类型,任意类都可以,其名称起到一个索引作用  
  72.                         0,                                        //ID号,这个数据比较有用,不同ID号代表不同数据  
  73.                         ExtendedWeakReference.c_SurvivePowerdown);//该标志和.c_SurviveBoot 区别不大  
  74.                 ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;  
  75.    
  76.                 FlashDatas data = ewr.Target as FlashDatas;  
  77.                 if (data == null)  
  78.                 {  
  79.                     data = new FlashDatas();  
  80.                 }  
  81.                 return data;  
  82.             }           
  83.    
  84.             //保存数据  
  85.             public void Save()  
  86.             {  
  87.                 ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(typeof(FlashDatas), 0, ExtendedWeakReference.c_SurvivePowerdown);  
  88.                 ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;  
  89.                 ewr.Target = this;  
  90.             }  
  91.         }  
  92.     }  
  93. }  

以上代码在Digi开发板上测试成功,断电之后,再上电,保存的数据确实没有丢失。
MSDN中相关函数的说明如下:
ExtendedWeakReference Members
The following tables list the members exposed by the ExtendedWeakReference type.
Public Constructors
  Name Description
ExtendedWeakReference Initializes a new instance of the ExtendedWeakReference class, referencing a specified object.

Public Fields
  Name Description
   c_SurviveBoot Contains a flag specifying that the current weak reference will be recoverable after the device reboots.
   c_SurvivePowerdown Contains a flag specifying that the current weak reference will be recoverable after the device powers down and restarts.

Public Properties
  Name Description
Flags Gets the flags specifying the states from which the current weak reference should be recoverable.
Id Gets the ID associated with the current ExtendedWeakReference object.
Priority Gets or sets the priority level for the current ExtendedWeakReference object. 
Selector Gets the selector for the current ExtendedWeakReference object.

Public Methods
  Name Description
PushBackIntoRecoverList Flags an ExtendedWeakReference object as a candidate for recovery after the device reboots or powers down and restarts.
   Recover Recovers a specific ExtendedWeakReference object.
   RecoverOrCreate Attempts to recover a specific ExtendedWeakReference object, and creates a new instance of this class if the recovery attempt fails.