Skip to main content
 首页 » 编程设计

c#之Windows Phone map 不断崩溃

2024年01月01日40fff_TT

我正在创建一个电话应用程序来映射您的位置以及其他 Assets 。手机通过 WI-FI 获取其他 Assets 位置的更新,并在用户选择的设定时间 - 通常为 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(); 
    } 
} 

我是线程的新手,我相信这可能是问题所在。感谢您的帮助。

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

请您参考如下方法:

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

看起来一切似乎都很好——只要手机移动了,代码就会执行;我在我的应用程序中使用了几乎相同的逻辑。