2014-12-03 4 views
0

MVVM 패턴에 문제가 있습니다. I는 해당 광고 온도 = Convert.ToDouble (args.Reading.HeadingMagneticNorth)에 debugingWindows Phone 8.1의 Compass COMException OnPropertyChanged 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 
} 

; 그것은 결과를 얻을 수 있지만, OnPropertyChanged를 내 다른 방법은 예외가 발생합니다 : 함께 System.Runtime.InteropServices.COMException 당신이 실버 라이트 윈도우 폰 응용 프로그램을하고 있다면 당신은 UI 스레드에 PropertyChanged 이벤트를 발생 할 필요가

+0

Silverlight 또는 Universal 응용 프로그램에 있나요? –

+0

Windows phone에서 COMException이 발생하는 이유는 무엇입니까? –

+0

Windows 용 Windows Phone 응용 프로그램입니다. – Criss

답변

0

, 다음 조금 같은 것을 할 수 있습니다 : 당신이 더 현대적인 보편적 인 스타일 앱을 사용하는 경우

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

그것은 일하고있다 - 고마워! – Criss

관련 문제