C# Windows phone地图不断崩溃
我正在创建一个电话应用程序,它可以映射出您的位置以及其他资产。手机通过WI-FI获取其他资产位置的更新,并在用户选择的设定时间内(通常为5秒)进行更新。问题是手机会在4分钟左右冻结在任何地方。我能做些什么来修复它 这是我的代码:C# Windows phone地图不断崩溃,c#,multithreading,windows-phone-7,bing-maps,freeze,C#,Multithreading,Windows Phone 7,Bing Maps,Freeze,我正在创建一个电话应用程序,它可以映射出您的位置以及其他资产。手机通过WI-FI获取其他资产位置的更新,并在用户选择的设定时间内(通常为5秒)进行更新。问题是手机会在4分钟左右冻结在任何地方。我能做些什么来修复它 这是我的代码: public bool CenterOnUser { get; set; } public MapView() { InitializeComponent(); CenterOnUser = false;
public bool CenterOnUser { get; set; }
public MapView()
{
InitializeComponent();
CenterOnUser = false;
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
_continue = true;
mapBing.Children.Add(_mapLayer);
_time = Convert.ToInt32(PhoneApplicationService.Current.State["time"]);
_thread = new Thread(Update) {Name = "Map Update"};
_thread.Start();
foreach (var asset in DataManager.GetPhoneAssetDictionary())
{
_averageLatitude += double.Parse(asset.Value.Latitude);
_averageLongitude += double.Parse(asset.Value.Longitude);
}
_averageLatitude /= DataManager.GetPhoneAssetDictionary().Count;
_averageLongitude /= DataManager.GetPhoneAssetDictionary().Count;
mapBing.Center = new GeoCoordinate(_averageLatitude, _averageLongitude);
}
public void Update()
{
while (_thread.IsAlive && _continue)
{
_mapLayer.Dispatcher.BeginInvoke(delegate
{
foreach (var asset in DataManager.GetPhoneAssetDictionary())
{
_position = new GetPhoneGPS().Positions();
// Find out if we already have a pin on the map for this asset
var pin = _mapLayer.Children.Cast<Pushpin>().FirstOrDefault(assetPin => assetPin.Tag.ToString() == asset.Key);
if (pin == null) // do we have a pin
{
// We don't yet. Create one
pin = new Pushpin {Tag = asset.Key};
// Figure out the name to give the pin
if (asset.Key == _position.UniqueId)
{
pin.Content = "You are here";
}
else
{
pin.Content = String.IsNullOrEmpty(asset.Value.Name) ? asset.Key : asset.Value.Name;
}
// Add the pin for this asset to the map
_mapLayer.Children.Add(pin);
}
// Always update the pin location
pin.Location = new GeoCoordinate(double.Parse(asset.Value.Latitude), double.Parse(asset.Value.Longitude));
}
});
Thread.Sleep(_time * 1000);
}
}
private void CenterOnUserBtn_Click(object sender, RoutedEventArgs e)
{
CenterOnUser = true;
mapBing.Center = new GeoCoordinate(double.Parse(_position.Latitude), double.Parse(_position.Longitude));
mapBing.ZoomLevel = 15;
}
private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
mapBing.Children.Remove(_mapLayer);
_continue = false;
_thread.Join();
}
}
public bool CenterOnUser{get;set;}
公共地图视图()
{
初始化组件();
CenterOnUser=false;
}
已加载专用void PhoneApplicationPage_(对象发送方,路由目标)
{
_继续=真;
mapping.Children.Add(_mapLayer);
_time=Convert.ToInt32(PhoneApplicationService.Current.State[“time”]);
_线程=新线程(更新){Name=“Map Update”};
_thread.Start();
foreach(DataManager.GetPhoneAssetDictionary()中的var资产)
{
_averageLatitude+=double.Parse(asset.Value.Latitude);
_Average经度+=double.Parse(asset.Value.Longitude);
}
_averageLatitude/=DataManager.GetPhoneAssetDictionary().Count;
_Average经度/=DataManager.GetPhoneAssetDictionary().Count;
mapping.Center=新的地理坐标(_平均纬度,_平均经度);
}
公共无效更新()
{
while(_thread.IsAlive&&u continue)
{
_mapLayer.Dispatcher.BeginInvoke(委托
{
foreach(DataManager.GetPhoneAssetDictionary()中的var资产)
{
_位置=新的GetPhoneGPS().Positions();
//了解我们是否已经在地图上为该资产设置了pin
var pin=\u mapLayer.Children.Cast().FirstOrDefault(assetPin=>assetPin.Tag.ToString()==asset.Key);
如果(pin==null)//我们有pin吗
{
//我们还没有。创建一个
pin=新图钉{Tag=asset.Key};
//找出要提供pin的名称
if(asset.Key==\u position.UniqueId)
{
pin.Content=“你在这里”;
}
其他的
{
pin.Content=String.IsNullOrEmpty(asset.Value.Name)?asset.Key:asset.Value.Name;
}
//将此资源的pin添加到地图
_mapLayer.Children.Add(pin);
}
//始终更新pin位置
pin.Location=新地理坐标(double.Parse(asset.Value.Latitude)、double.Parse(asset.Value.Longitude));
}
});
线程睡眠(_时间*1000);
}
}
私有void centerOnSerbtn\u单击(对象发送方,路由目标)
{
CenterOnUser=true;
mapping.Center=新地理坐标(double.Parse(_位置.纬度),double.Parse(_位置.经度));
mapping.ZoomLevel=15;
}
私有void PhoneApplicationPage_已卸载(对象发送方,路由目标)
{
mapping.Children.Remove(_mapLayer);
_continue=false;
_thread.Join();
}
}
我是线程方面的新手,我相信这可能就是问题所在。谢谢你的帮助
这是getPhone的代码
public class GetPhoneGPS : IDisposable
{
private static readonly Positions Position = new Positions();
readonly GeoCoordinateWatcher _watch = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
private void UpdatePhonePosition()
{
_watch.MovementThreshold = 1;
_watch.Start();
_watch.PositionChanged += GeoPositionChange;
var position = _watch.Position;
//loads phones gps coordinates
if (!position.Location.IsUnknown)
{
Position.Latitude = position.Location.Latitude.ToString(CultureInfo.InvariantCulture);
Position.Longitude = position.Location.Longitude.ToString(CultureInfo.InvariantCulture);
Position.Altitude = position.Location.Altitude.ToString(CultureInfo.InvariantCulture);
Position.Speed = position.Location.Speed.ToString(CultureInfo.InvariantCulture);
}
Position.Date = DateTime.Now;
Position.Name = PhoneApplicationService.Current.State["username"].ToString();
Position.UniqueId = PhoneApplicationService.Current.State["UniqueId"].ToString();
}
private void GeoPositionChange(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
var position = e.Position;
//loads phones gps coordinates
if (!position.Location.IsUnknown)
{
Position.Latitude = position.Location.Latitude.ToString(CultureInfo.InvariantCulture);
Position.Longitude = position.Location.Longitude.ToString(CultureInfo.InvariantCulture);
Position.Altitude = position.Location.Altitude.ToString(CultureInfo.InvariantCulture);
Position.Speed = position.Location.Speed.ToString(CultureInfo.InvariantCulture);
}
Position.Date = DateTime.Now;
}
public Positions Positions()
{
UpdatePhonePosition();
return Position;
}
public void Dispose()
{
_watch.Dispose();
}
}
公共类GetPhoneGPS:IDisposable
{
私有静态只读位置位置=新位置();
readonly GeoCoordinateWatcher _watch=新的GeoCoordinateWatcher(地理位置精度高);
私有void UpdatePhonePosition()
{
_watch.MovementThreshold=1;
_watch.Start();
_watch.PositionChanged+=地理位置更改;
变量位置=_watch.position;
//加载gps坐标
如果(!position.Location.IsUnknown)
{
Position.Latitude=Position.Location.Latitude.ToString(CultureInfo.InvariantCulture);
Position.Longitude=Position.Location.Longitude.ToString(CultureInfo.InvariantCulture);
Position.altime=Position.Location.altime.ToString(CultureInfo.InvariantCulture);
Position.Speed=Position.Location.Speed.ToString(CultureInfo.InvariantCulture);
}
Position.Date=DateTime.Now;
Position.Name=PhoneApplicationService.Current.State[“username”].ToString();
Position.UniqueId=PhoneApplicationService.Current.State[“UniqueId”].ToString();
}
私有void geoppositionchange(对象发送方,geoppositionchangedventargs e)
{
var位置=e位置;
//加载gps坐标
如果(!position.Location.IsUnknown)
{
Position.Latitude=Position.Location.Latitude.ToString(CultureInfo.InvariantCulture);
Position.Longitude=Position.Location.Longitude.ToString(CultureInfo.InvariantCulture);
Position.altime=Position.Location.altime.ToString(CultureInfo.InvariantCulture);
Position.Speed=Position.Location.Speed.ToString(CultureInfo.InvariantCulture);
}
Position.Date=DateTime.Now;
}
公共职位
{
UpdatePhonePosition();
返回位置;
}
公共空间处置()
{
_watch.Dispose();
}
}
这是在您通过模拟器使用它时,还是在开发模式下将应用程序发布到手机后?我发现有一些模拟器不喜欢更新的实例,但它们很少
看着一切似乎都没问题——只要手机移动了,代码就会执行;我的应用程序使用了几乎相同的逻辑。什么是“GetPhoneGPS()”?也许是w