Windows手机地图不断崩溃

时间:2012-11-29 20:19:58

标签: c# multithreading windows-phone-7 bing-maps freeze

我正在创建一个电话应用程序,用于映射您的位置以及其他资产。手机通过WI-FI获取其他资产的位置更新,以及用户选择的设定时间 - 通常为5秒。手机将在4分钟左右冻结的问题。我该怎么做才能解决它?

这是我的代码:

    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();
    }
}

我是Threading的新手,我相信这可能是问题所在。谢谢你的帮助。

这是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();
        }
    }

1 个答案:

答案 0 :(得分:0)

这是通过模拟器使用它还是在开发模式下将应用程序发布到手机时?我发现有些情况下模拟器不喜欢更新,但它们很少,而且很少。

看一下这一切似乎都没问题 - 只要手机移动,代码就会执行;我在我的应用程序中使用了几乎相同的逻辑。