0

私は、10秒ごとに加速度計の読み取り値を永続的なファイルストレージ領域に記録できるWindows phoneアプリケーションを開発しようとしています.

そのために、加速度計の読み取り値を取得して個別に表示するプログラムコードを作成しました(x読み取り用のテキストブロック、y読み取り用のテキストブロック、z読み取り用のテキストブロック)。次に、これらすべての読み取り値を Textbox にまとめて保存し、TextChanged イベントを使用して変更をファイルに記録します。読み取り値は、分離ファイル ストレージ領域に保存されます。

さまざまな機能は正常に動作していますが、私が直面している問題は、ファイルが加速度計の最後の値のみを表示していること、つまり、テキストボックスが最後に読み取った値のみを保存していることです。

なぜこれが起こっているのか、これを解決するために何ができるのか、誰かが提案できますか? 私が使用したコードは次のとおりです。

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();
            }
        }
    }
4

1 に答える 1

0

あなたのコードをコピーして貼り付けましたが、問題なく動作します。値のテキストブロックのサイズを変更し、textwrapping プロパティをラップに設定してみてください。おそらくそれが表示されない理由です

また、毎回新しい Dispatchertimer オブジェクトを使用せず、初期化して開始すると、アプリ全体で 1 つだけを持つことができます

それがあなたを助けることを願っています!

于 2012-09-25T06:20:42.697 に答える