INotifyPropertyChanged 不起作用

INotifyPropertyChanged does not work

可能不是最好的解决方案,但我注意到我的应用程序的许多不同页面和用户控件使用相同的数据(主要是通过绑定),所以我做了以下 class

public class GPSHelper : INotifyPropertyChanged
    {
        private double _speed;
        public double Speed
        {
            get
            {
                return _speed;
            }
            set
            {
                _speed = value;
                NotifyPropertyChanged("Speed");
            }
        }
        public double AvgSpeed { get; set; }
        public double MaxSpeed { get; set; }
        public double Distance { get; set; }
        public double Altitude { get; set; }
        public double Longtitude { get; set; }
        public double Latitude {get; set;}
        private int _locationChangedCounter;
        private Geolocator _locator; 
        public GPSHelper()
        {
            _locator = new Geolocator();
            _locator.MovementThreshold = 0.5;
            _locator.PositionChanged += locator_PositionChanged;
            MaxSpeed = 0;
            AvgSpeed = 0;
            Speed = 30; 
        }

        private async void locator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {

            Geoposition position = await _locator.GetGeopositionAsync();
            Geopoint point = args.Position.Coordinate.Point;
            //Speed = position.Coordinate.Speed.Value;
            Speed = 120; 
            Altitude = point.Position.Altitude;
            if(Speed > MaxSpeed)
            {
                MaxSpeed = Speed; 
            }
            AvgSpeed += Speed / _locationChangedCounter;
            _locationChangedCounter++;
        }

        private void NotifyPropertyChanged(string propertyname)
        {
            if(PropertyChanged!=null)
            {
                 PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
    }

并且在 App.xaml 中我添加了 class 作为资源,我不知道我是否可以这样做,但它似乎仍然是一个不错的主意。

<Application
    x:Class="SpeedometerGPS.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:SpeedometerGPS"
    xmlns:helpers ="using:SpeedometerGPS.Helpers">
    <Application.Resources>
        <helpers:GPSHelper x:Key="GPSHelper" /> 
    </Application.Resources>
</Application>

我现在遇到的唯一问题是 PropertyChanged 无法工作,它给出了一个非常合理的理由,为什么 - ,应用程序调用了一个为不同线程编组的接口。”关于如何解决的任何建议是吗?

问题是传入的事件不在 GUI 线程上,而是在工作线程上。 然后您正在操纵速度,我认为这是某些绑定的一部分。 如果它是某些绑定的一部分,则更新速度将对这些绑定产生影响,但这仅在 GUI 中发生更改时才有效。

最重要的是,您必须将速度的变化或整个事件发送到 GUI 线程。

此处解释了向 GUI 线程的分派: The application called an interface that was marshalled for a different thread in window 8

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    Speed = 120; 
});

Geolocator 文档说:

"Important Call the RequestAccessAsync before accessing the user’s location. At that time, your app must be in the foreground and RequestAccessAsync must be called from the UI thread. Until the user grants your app permission to their location, your app can't access location data."

https://msdn.microsoft.com/en-us/library/windows/apps/windows.devices.geolocation.geolocator

因此,您应该将地理定位器作为静态 属性 在其他地方(例如 App.xaml.cs),并在获取任何事件之前从 UI 线程中的方法调用 RequestAccessAsync。

我自己解决了这个问题,在 App.xaml.cs 中将 Geolocator 作为静态成员并实际首先在 OnLaunch() 中使用它,然后分配事件处理程序解决了一段时间的问题,但整个程序在一分钟左右后崩溃。在谷歌上搜索了几个小时后发现,为了按照我希望的方式使用 class 中的调度程序,你必须做这样的事情。然而,感谢你们所有人的努力,无论你是否正在阅读。

await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority,() => {})