Windows Phone 8.1 中的指南针 COMException OnPropertyChanged MVVM.

本文关键字:指南针 COMException OnPropertyChanged MVVM Phone Windows | 更新日期: 2023-09-27 18:33:07

我的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
}

当我在那行调试时 temp = Convert.ToDouble(args.阅读.标题磁北);它得到结果,但我的另一个方法 OnPropertyChanged 抛出异常: System.Runtime.InteropServices.COMException

Windows Phone 8.1 中的指南针 COMException OnPropertyChanged MVVM.

你需要在

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

如果您使用的是更现代的通用样式应用程序,则只需将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);
            }
        }
    }