.Net Micro Framework不支持文件系统(目前该项功能正在研发之中),所以无法像Windows和windows ce平台那样把需要永久保存的数据保存到文件之中。内存中保存的数据只要系统一掉电,所有的数据也都消失了,这对一些需要保存参数的应用来说真是不妙。
这几天在研究MF触摸屏功能时就遇到该问题,因为触摸屏校准之后,需要保存校准后的参数,否则MF一重启,难道还需要重新校准不成?
感谢Donald Thompson 和 Rob S. Miles,从他们的大作上找到了问题的解决办法。办法就是把对象保存到Flash(EEPROM)中(有点像对象的二进制序列化)。
下面是我整理的示例代码(实现比较简单,但总觉得不太正规,不知道能存多大,也搞不清楚数据到底存放在什么位置了。):
using System;
using Microsoft.SPOT;
using System.Collections;
using System.Threading;
namespace DataStorage
{
public class Program
{
public static void Main()
{
FlashDatas fd = FlashDatas.Load();
fd.dump();
fd.Flag = "adfg";
//fd.Items.Clear();
//fd.AddItem(new FlashData(55, "1aaa"));
//fd.AddItem(new FlashData(66, "2bbb"));
//fd.AddItem(new FlashData(77, "3ccc"));
//fd.AddItem(new FlashData(88, "4ddd"));
fd.Save();
Thread.Sleep(3000);
}
[Serializable]
private class FlashData
{
public DateTime date;
public int iData;
public string sData;
public FlashData(int iData, string sData)
{
date = DateTime.Now;
this.iData = iData;
this.sData = sData;
}
public override string ToString()
{
return date.ToString() + " " + iData.ToString() + " " + sData;
}
}
[Serializable]
private class FlashDatas
{
public DateTime CreateTime = DateTime.Now;
public string Flag = @"http://blog.csdn.net/yefanqiu/";
public ArrayList Items = new ArrayList();
public void dump()
{
Debug.Print(CreateTime.ToString());
Debug.Print(Flag);
foreach (FlashData Item in Items)
{
if (Item != null)
{
Debug.Print(Item.ToString());
}
}
}
public void AddItem(FlashData Item)
{
Items.Add(Item);
}
//调入数据
public static FlashDatas Load()
{
ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(
typeof(FlashDatas), //类型,任意类都可以,其名称起到一个索引作用
0, //ID号,这个数据比较有用,不同ID号代表不同数据
ExtendedWeakReference.c_SurvivePowerdown);//该标志和.c_SurviveBoot 区别不大
ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;
FlashDatas data = ewr.Target as FlashDatas;
if (data == null)
{
data = new FlashDatas();
}
return data;
}
//保存数据
public void Save()
{
ExtendedWeakReference ewr = ExtendedWeakReference.RecoverOrCreate(typeof(FlashDatas), 0, ExtendedWeakReference.c_SurvivePowerdown);
ewr.Priority = (Int32)ExtendedWeakReference.PriorityLevel.Important;
ewr.Target = this;
}
}
}
}
以上代码在Digi开发板上测试成功,断电之后,再上电,保存的数据确实没有丢失。
MSDN中相关函数的说明如下:
ExtendedWeakReference Members
The following tables list the members exposed by the ExtendedWeakReference type.
Public Constructors
Public Fields
|
Name
|
Description
|
|
|
Contains a flag specifying that the current weak reference will be recoverable after the device reboots.
|
|
|
Contains a flag specifying that the current weak reference will be recoverable after the device powers down and restarts.
|
Public Properties
|
Name
|
Description
|
|
Gets the flags specifying the states from which the current weak reference should be recoverable.
|
|
|
|
|
|
|
Public Methods
|
Name
|
Description
|
|
Flags an ExtendedWeakReference object as a candidate for recovery after the device reboots or powers down and restarts.
|
|
|
|
|
|
Attempts to recover a specific ExtendedWeakReference object, and creates a new instance of this class if the recovery attempt fails.
|
分享到:
相关推荐
The Microsoft .NET Micro Framework is a small and efficient .NET runtime environment used to run managed code on devices that are too small and resource constrained for Windows CE and the Compact ...
《玩转.NET Micro Framework移植-基于STM32F10x处理器》源程序
.NET Micro Framework 电子书
.net micro framework关于文件操作的例子,非常详细。还包括了点击等东西
expert .net micro frameworkexpert .net micro frameworkexpert .net micro framework
.NET Micro Framework自动化测试工具,需.NET Micro Framework3.0和.NET Micro Framework Test Kit支持...
Microsoft .NET Micro Framework 2.5 SDK。 这个我暂时没用,权当放在这里存放,需要的,自己动手。
近来有些刚入门的用户,不太了解.NET Micro Framework开发板的使用,所以写了这篇文档,以期缩短用户对开发板的熟悉时间。
micro framework 4.0 SDK,最新开发工具包
这是我自己改造的.Net Micro Framework模拟器,比windows自带的漂亮多了。 覆盖该目录下的同名文件(路径以实际开发包安装位置为准)即可。 C:\Program Files\Microsoft .NET Micro Framework\v2.0.3036\Tools 支持...
.NET Micro Framework 电子书
http://item.taobao.com/item.htm?id=7117999726 .NET Micro Framework开发板的Key文件的部署说明。
.NET Micro Framework开发板原理图(红牛开发板)
.NET Micro Framework开发板使用手册(红牛开发板)
如何顺利编译.Net Micro Framework Porting Kit 4.1 安装路径不能有空格
《玩转.NET Micro Framework 移植-基于STM32F10x处理器》一书所有的源代码。其它更多的资源可以访问我的blog:http://blog.csdn.net/norains 谢谢!
.NET Micro Framework for Windows 7(X64) USB驱动安装说明,包含相应的驱动程序
注:比上一个模拟器多了支持方向键控制 这是我自己改造的.Net Micro Framework模拟器,比windows自带的漂亮多了。 覆盖该目录下的同名文件(路径以实际开发包安装位置为准)即可。 C:\Program Files\...
GHI .NET Micro Framework培训教材