【发布时间】:2015-04-14 13:37:26
【问题描述】:
可能不是最好的解决方案,但我注意到我的应用程序的许多不同页面和用户控件使用相同的数据(主要是通过绑定),所以我做了以下类
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 中,我将类添加为资源,我不知道是否可以这样做,但这似乎仍然是个好主意。
<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 不起作用,它给出了一个非常正当的理由 - ,,应用程序调用了一个为不同线程编组的接口。”有什么建议可以解决它吗?
【问题讨论】:
标签: c# xaml geolocation windows-phone-8.1