簡體   English   中英

Windows Phone 8.1中的指南針COMException OnPropertyChanged MVVM

[英]Compass in Windows Phone 8.1 COMException OnPropertyChanged MVVM

我的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.Reading.HeadingMagneticNorth); 它得到結果,但是我的其他方法OnPropertyChanged引發異常:System.Runtime.InteropServices.COMException

您需要在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);
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM