我使用
在Geolocator对象中设置PositionChanged侦听器var geolocator = new Geolocator();
geolocator.PositionChanged += Geolocator_PositionChanged;
一段时间以来一切正常。但经过一段时间没有用户互动(+ - 4小时)它停止接收位置变化,我认为因为WP8只是杀死它。这可能是从ms开始的,但这对我的应用模型来说太可怕了。
我所做的是另外设置PeriodicTask并发送位置。这样运行没有任何问题,但如果用户改变他的位置,我就无法真正跟踪它。
问题是:无论如何在不需要用户交互的情况下唤醒这个Geolocator?它可以通过PeriodicTask,甚至可以通过PositionChanged。
我已经尝试在PositionChanged委托中已经实例化了一个新的Geolocator,但这不起作用。
非常感谢任何帮助。谢谢!
答案 0 :(得分:2)
似乎某种GC可以杀死/禁用该实例。
尝试使用geolocator.addEventListener("statuschanged", onStatusChanged);
并在状态为Windows.Devices.Geolocation.PositionStatus.disabled
或Windows.Devices.Geolocation.PositionStatus.notAvailable
时重新启动对象。
有关详细信息,请参阅Geolocator.StatusChanged | statuschanged event。
希望我帮助
答案 1 :(得分:1)
您是否在GetGeopositionAsync
来电中指定了超时?根据{{3}}底部的最后一条评论,此代码修复了GetGeopositionAsync
调用未返回的问题。它可能是相关的。
public Task GetCurrentPosition()
{
return Task.Run(() =>
{
if (_geolocator == null)
{
_geolocator = new Geolocator { DesiredAccuracy = PositionAccuracy.High };
}
Geoposition geoposition = null;
ManualResetEvent syncEvent = new ManualResetEvent(initialState: false);
IAsyncOperation asyncOp = _geolocator.GetGeopositionAsync();
asyncOp.Completed += (asyncInfo, asyncStatus) =>
{
if (asyncStatus == AsyncStatus.Completed)
{
geoposition = asyncInfo.GetResults();
}
syncEvent.Set();
};
syncEvent.WaitOne();
return geoposition.Coordinate;
});
}
答案 2 :(得分:1)
可能垃圾收集器发现它未使用并且已杀死或至少禁用省电。
您使用的大多数硬件资源都有名为
的方法addEventListener
这是告知该资源的一种方式:“嘿!我想得到你的更新,所以不要死”。这是观察对象并异步获取更新的非常常见的模式。