这里有点难过。
我有一个每 10 秒触发一次的 TimerCallback,其中包含要放在地图上的地理点。我尝试从计时器回调函数中将这些添加到地图中,但是由于它位于不同的线程中,因此我无法执行此操作。我收到以下错误:
mscorlib.ni.dll 中发生了“System.IO.FileNotFoundException”类型的异常,并且在托管/本机边界之前未处理。
System.Windows.ni.dll 中出现“System.UnauthorizedAccessException”类型的第一次机会异常
我该如何规避呢?我认为可能添加一个 NotifyCollectionChanged 侦听器会起作用,但是我仍然遇到同样的问题。代码如下所示。
private ObservableCollection<Bus> _busList;
private Timer _timer = null;
public ItemViewModel route;
public ObservableCollection<Bus> BusList
{
get { return _busList; }
set { _busList = value; }
}
//public LocationManager locMan = LocationManager.Instance;
// Constructor
public DetailsPage()
{
InitializeComponent();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
// When page is navigated to set data context to selected item in list
protected override void OnNavigatedTo(NavigationEventArgs e)
{
route = null;
if (DataContext == null)
{
string selectedIndex = "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
{
int index = int.Parse(selectedIndex);
DataContext = App.ViewModel.Items[index];
route = App.ViewModel.Items[index];
if (_timer == null)
{
TimerCallback tcb = obtainJSON;
_timer = new Timer(tcb, route.RouteID, 0, 10000);
}
else
{
_timer.Change(0, 10000);
}
if (BusList == null)
{
BusList = new ObservableCollection<Bus>();
}
else
{
BusList.Clear();
}
BusList.CollectionChanged += HandleBusAdded;
}
}
}
private void HandleBusAdded(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
Debug.WriteLine("Everything was cleared");
}
else
{
foreach (Bus item in e.NewItems)
{
Debug.WriteLine(item.vehicleID);
Polygon polygon = new Polygon();
polygon.Points.Add(new Point(0, 0));
polygon.Points.Add(new Point(0, 75));
polygon.Points.Add(new Point(25, 0));
polygon.Fill = new SolidColorBrush(Colors.Blue);
// Create a MapOverlay and add marker
MapOverlay overlay = new MapOverlay();
overlay.Content = polygon;
overlay.GeoCoordinate = new GeoCoordinate(item.lat, item.lng);
overlay.PositionOrigin = new Point(0.0, 1.0);
MapLayer mapLayer = new MapLayer();
mapLayer.Add(overlay);
//mapView.Layers.Add(mapLayer);
this.Dispatcher.BeginInvoke(() =>
{
mapView.Layers.Add(mapLayer);
});
}
}
}
public void obtainJSON(Object stateInfo)
{
string url = "http://xxx.xxx.xxx" + stateInfo.ToString();
var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Decrypt);
client.DownloadStringAsync(new Uri(url));
}
public void Decrypt(Object sender, DownloadStringCompletedEventArgs e)
{
if (BusList.Count > 0)
{
BusList.Clear();
}
if (!e.Cancelled && e.Error == null)
{
var temp = new List<string>();
string[] buses = e.Result.Split('\n');
foreach (string bus in buses)
{
if (!string.IsNullOrWhiteSpace(bus) && !string.IsNullOrEmpty(bus))
{
temp.Add(bus);
}
}
foreach (string item in temp)
{
string[] busInfo = item.Split(',');
Bus newBus = new Bus(busInfo[0], busInfo[1], busInfo[2]);
BusList.Add(newBus);
}
// This is where I initially tried
/*Polygon polygon = new Polygon();
polygon.Points.Add(new Point(0, 0));
polygon.Points.Add(new Point(0, 75));
polygon.Points.Add(new Point(25, 0));
polygon.Fill = new SolidColorBrush(Colors.Blue);
// Enable marker to be tapped for location information
// polygon.Tag = new GeoCoordinate(BusList[0].lat, BusList[0].lng);
// Create a MapOverlay and add marker
MapOverlay overlay = new MapOverlay();
overlay.Content = polygon;
overlay.GeoCoordinate = new GeoCoordinate(BusList[0].lat, BusList[0].lng);
overlay.PositionOrigin = new Point(0.0, 1.0);
MapLayer mapLayer = new MapLayer();
mapLayer.Add(overlay);
this.Dispatcher.BeginInvoke(() =>
{
mapView.Layers.Add(mapLayer);
});*/
Debug.WriteLine("Present buses " + BusList[0].vehicleID + ", " + BusList[1].vehicleID);
}
}
}