简体   繁体   English

如何在Windows Phone的永久性存储文件中记录加速度计读数

[英]How to record accelerometer readings in a persistent storage file on a windows phone

``I am trying to develop a Windows phone application which can record accelerometer readings after every 10 secs or so to a persistent file storage area. ``我正在尝试开发一个Windows Phone应用程序,该应用程序可以每隔10秒左右记录一次加速度计读数到一个永久文件存储区域。

For that, I have written a program code which gets the accelerometer readings and displays them seperately( one textblock for x reading, one for y reading and one for z reading). 为此,我编写了一个程序代码,该程序代码获取加速度计读数并单独显示(一个文本块用于x读取,一个文本块用于y读取,一个用于z读取)。 Then I store all these readings together in a Textbox and use the TextChanged event to record the changes to the file. 然后,我将所有这些读数一起存储在文本框中,并使用TextChanged事件记录对文件的更改。 The readings are stored to an Isolated File storage area. 读数将存储到隔离文件存储区。

The different functions are working fine but the problem I am facing is that the file is showing only the last value of acccelerometer read or in other words, the textbox is saving only the last value read. 不同的功能工作正常,但我面临的问题是文件仅显示了加速度计读取的最后一个值,换句话说,文本框仅保存了读取的最后一个值。

Can anyone suggest why is this happenning and what can I do to solve this? 谁能说出为什么会发生这种情况,我该怎么解决呢? The code I used is as follows: 我使用的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Devices.Sensors;
using Microsoft.Xna.Framework;
using System.Windows.Threading;
using System.IO.IsolatedStorage;
using System.IO;

namespace PhoneApp1
{
    public partial class MainPage : PhoneApplicationPage
    {
        Accelerometer a;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            if (!Accelerometer.IsSupported)
            {
                status.Text = "Sensor not supported";
                start.IsEnabled = false;
                stop.IsEnabled = false;
            }
            }

        private void start_Click(object sender, RoutedEventArgs e)
        {
            if (a == null)
            {
                a = new Accelerometer();

            }
            try
            {
                status.Text = "Starting Accelerometer";
                a.TimeBetweenUpdates = TimeSpan.FromMilliseconds(1000);
                a.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(a_CurrentValueChanged);
                a.Start();
            }
            catch (Exception e1)
            {
                status.Text = e1.Message;
            }
        }

        private void stop_Click(object sender, RoutedEventArgs e)
        {
            if (a != null)
            {
                a.Stop();
                status.Text = "Accelerometer stopped";
            }
            else
            {
                status.Text = "Accelerometer not started";
            }
        }
        void a_CurrentValueChanged(Object sender, SensorReadingEventArgs<AccelerometerReading> sr)
        {
            Dispatcher.BeginInvoke(() => UpdateUI(sr.SensorReading));
        }
        void UpdateUI(AccelerometerReading ar)
        {
            DispatcherTimer newTimer = new DispatcherTimer();
            newTimer.Interval = TimeSpan.FromSeconds(1);
            status.Text = "Getting Accelerometer Readings";
            Vector3 xyz = ar.Acceleration;
            values.Text = "X: " + xyz.X.ToString("0.00") + "\t" + "Y: " + xyz.Y.ToString("0.00") + "\t" + "Z: " + xyz.Z.ToString("0.00");
            newTimer.Tick += OnTimerTick;
            newTimer.Start();

        }
        void OnTimerTick(Object sender, EventArgs args)
        {
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            isoStore.CreateDirectory("Accelerometer");
            IsolatedStorageFileStream fileStream=isoStore.OpenFile("Accelerometer\\myFile.txt",FileMode.Append,FileAccess.Write);
            using(StreamWriter writeFile=new StreamWriter(fileStream))
            {
            writeFile.WriteLine(values.Text);
            writeFile.Close();
            }
        }

        private void read_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("Accelerometer\\myFile.txt", FileMode.Open, FileAccess.Read);
            fileStream.Position = 0;
            using (StreamReader reader = new StreamReader(fileStream))
            {
                values.Text += "\n" + reader.ReadToEnd();
            }
        }
    }

I copy-pasted your code and it works fine for me. 我复制粘贴了您的代码,对我来说效果很好。 Try resizing the values textblock and setting the textwrapping property to wrap, maybe that is the reason you dont see them 尝试调整值textblock的大小并设置textwrapping属性以换行,也许这就是您看不到它们的原因

Also I wouldnt use a new Dispatchertimer object every time, initalizing, and starting, you could have just one for the whole app 另外,我不会每次都使用一个新的Dispatchertimer对象,因此在启动和启动时,整个应用程序可能只有一个

hope that helps you! 希望对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM