加速度传感器
演示了使用Windows.Devices.Sensors。 加速度计的API。
这个简单的允许用户浏览加速军队沿X -,Y -,和一3-axis Z-axes加速度计。你可以选择三种情况之一:
•加速度计数据的事件
•加速度计摇事件
•现有加速度计阅读
Acclerometer数据事件
当你选择使按钮加速度计数据项目的选择,应用程序将开始流加速度传感器读数中实时。
加速度计摇事件
当你选择使按钮加速度计摇事件选项,这个应用程序显示的累积数目的震动的事件每一次事件发生。(这个应用程序首先增加事件计数,然后使最近的价值。)
现有加速度计阅读
当你选择让按钮为当前的加速度计阅读选项,这个应用程序将恢复最近的加速度计的阅读。
class SuspensionManager
{
static private Dictionary<string, object> sessionState_ = new Dictionary<string, object>();
static private List<Type> knownTypes_ = new List<Type>();
private const string filename = "_sessionState.xml";
// Provides access to the currect session state
static public Dictionary<string, object> SessionState
{
get { return sessionState_; }
}
// Allows custom types to be added to the list of types that can be serialized
static public List<Type> KnownTypes
{
get { return knownTypes_; }
}
// Save the current session state
static async public Task SaveAsync()
{
// Get the output stream for the SessionState file.
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (IOutputStream outStream = raStream.GetOutputStreamAt(0))
{
// Serialize the Session State.
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_);
serializer.WriteObject(outStream.AsStreamForWrite(), sessionState_);
await outStream.FlushAsync();
}
}
// Restore the saved sesison state
static async public Task RestoreAsync()
{
// Get the input stream for the SessionState file.
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
if (file == null) return;
IInputStream inStream = await file.OpenSequentialReadAsync();
// Deserialize the Session State.
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_);
sessionState_ = (Dictionary<string, object>)serializer.ReadObject(inStream.AsStreamForRead());
}
catch (Exception)
{
// Restoring state is best-effort. If it fails, the app will just come up with a new session.
}
}
}
static private Dictionary<string, object> sessionState_ = new Dictionary<string, object>();
static private List<Type> knownTypes_ = new List<Type>();
private const string filename = "_sessionState.xml";
// Provides access to the currect session state
static public Dictionary<string, object> SessionState
{
get { return sessionState_; }
}
// Allows custom types to be added to the list of types that can be serialized
static public List<Type> KnownTypes
{
get { return knownTypes_; }
}
// Save the current session state
static async public Task SaveAsync()
{
// Get the output stream for the SessionState file.
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
IRandomAccessStream raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (IOutputStream outStream = raStream.GetOutputStreamAt(0))
{
// Serialize the Session State.
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_);
serializer.WriteObject(outStream.AsStreamForWrite(), sessionState_);
await outStream.FlushAsync();
}
}
// Restore the saved sesison state
static async public Task RestoreAsync()
{
// Get the input stream for the SessionState file.
try
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
if (file == null) return;
IInputStream inStream = await file.OpenSequentialReadAsync();
// Deserialize the Session State.
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), knownTypes_);
sessionState_ = (Dictionary<string, object>)serializer.ReadObject(inStream.AsStreamForRead());
}
catch (Exception)
{
// Restoring state is best-effort. If it fails, the app will just come up with a new session.
}
}
}
完整Demo