windows8 重力加速度感应
加速度传感器
演示了使用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
作者:risk
出处:http://www.cnblogs.com/risk
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://www.cnblogs.com/risk
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述