0

電話画面で自分の現在位置を確認したい。このプロジェクトでは MVVM モデルを使用します。

XAML

 <TextBlock Name="XBlock" 
                               Visibility="Visible"
                               Text="{Binding Path=XValue, Mode=OneWay}" 
                               Margin="0,0,359,0" Height="75"/>

ビューモデル

public string XValue {get ; set ; }
        public string YValue { get; set; }
        public string ZValue { get; set; }

        void _accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e) 
        {
            var position = e.SensorReading.Acceleration;

            SmartDispatcher.Dispatch(() =>
            {
                this.XValue = position.X.ToString("0.0000");
                this.YValue = position.Y.ToString("0.0000");
                this.ZValue = position.Z.ToString("0.0000");
            });
        }

ディスパッチャ クラス

public static Dispatcher DispatchObject { get; set; }

        public static void Dispatch(Action action)
        {
            if (DispatchObject == null || DispatchObject.CheckAccess())
            {
                action();
            }
            else
            {
                DispatchObject.InvokeAsync(action);
            }
        }

TextBlock が空です。加速度計からのデータは正しく読み取られます(Degugで確認済み)。

メソッド XValue を次のように変更する場合:

 public string XValue {
            get { return "ds"; }
            set{ XValue = "fd";} 
        }

TextBlock には「ds」があります。現在の加速度計データを表示するにはどうすればよいですか?

4

2 に答える 2

0

データがいつ変更され、更新できるかを が認識できるように、 にINotifyPropertyChangedインターフェイスを追加する必要があります。ViewModelXaml

例:

public class MyViewModel : INotifyPropertyChanged
{
    private string _zValue;
    private string _yValue;
    private string _xValue;

    public string XValue
    {
        get { return _xValue; }
        set { _xValue = value; NotifyPropertyChanged("XValue"); }
    }

    public string YValue
    {
        get { return _yValue; }
        set { _yValue = value; NotifyPropertyChanged("YValue"); }

    }
    public string ZValue
    {
        get { return _zValue; }
        set { _zValue = value; NotifyPropertyChanged("ZValue"); }
    }

    void _accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
    {
        var position = e.SensorReading.Acceleration;

        SmartDispatcher.Dispatch(() =>
        {
            this.XValue = position.X.ToString("0.0000");
            this.YValue = position.Y.ToString("0.0000");
            this.ZValue = position.Z.ToString("0.0000");
        });
    }


    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
于 2013-10-14T03:30:06.760 に答える