简体   繁体   English

Windows Phone 8.1中的指南针COMException OnPropertyChanged MVVM

[英]Compass in Windows Phone 8.1 COMException OnPropertyChanged MVVM

I got a problem with my MVVM pattern. 我的MVVM模式有问题。

class MagnetometrViewModel : INotifyPropertyChanged
{
    private Compass compass;
    double temp;
    public MagnetometrViewModel()
    {
        compass = Compass.GetDefault();
        CompassControl_Loaded();
    }

    private double heading;
    public double Heading
    {
        get
        {
            return heading;
        }
        set
        {
            heading = value;
            OnPropertyChanged("Heading");
        }
    }

    private void CompassControl_Loaded()
    {
        compass = Windows.Devices.Sensors.Compass.GetDefault();
        if (compass != null)
        {
            compass.ReadingChanged += CompassReadingChanged;
        }
    }

    private void CompassReadingChanged(Windows.Devices.Sensors.Compass sender, Windows.Devices.Sensors.CompassReadingChangedEventArgs args)
    {

         temp = Convert.ToDouble(args.Reading.HeadingMagneticNorth);
         Heading = temp;
         //Heading = Convert.ToDouble(args.Reading.HeadingMagneticNorth);
    }

    #region PropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

When I debuging on that line temp = Convert.ToDouble(args.Reading.HeadingMagneticNorth); 当我在那条线上调试时,temp = Convert.ToDouble(args.Reading.HeadingMagneticNorth); it get the result, but my other method OnPropertyChanged throws exception: System.Runtime.InteropServices.COMException 它得到结果,但是我的其他方法OnPropertyChanged引发异常:System.Runtime.InteropServices.COMException

You need to fire the PropertyChanged events on the UI thread, if you're doing a Silverlight Windows Phone app, then you can do something a little like: 您需要在UI线程上触发PropertyChanged事件,如果您正在执行Silverlight Windows Phone应用程序,则可以执行以下操作:

    protected delegate void OnUIThreadDelegate();
    /// <summary>
    /// Allows the specified delegate to be performed on the UI thread.
    /// </summary>
    /// <param name="onUIThreadDelegate">The delegate to be executed on the UI thread.</param>
    protected static void OnUIThread(OnUIThreadDelegate onUIThreadDelegate)
    {
        if (onUIThreadDelegate != null)
        {
            if (Deployment.Current.Dispatcher.CheckAccess())
            {
                onUIThreadDelegate();
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(onUIThreadDelegate);
            }
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        OnUIThread(() =>
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        });
    }

If you're using the more modern Universal style apps, then simply change the OnUIThread implementation to be something more like: 如果您使用的是更现代的通用风格的应用程序,则只需将OnUIThread实现更改为类似以下内容:

    protected async void OnUIThread(DispatchedHandler onUIThreadDelegate)
    {
        if (onUIThreadDelegate != null)
        {
            var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
            if (dispatcher.HasThreadAccess)
            {
                onUIThreadDelegate();
            }
            else
            {
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, onUIThreadDelegate);
            }
        }
    }

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

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