ここで少し困惑。
マップに配置するジオポイントを含む 10 秒ごとに起動する TimerCallback があります。これらをタイマー コールバック関数内からマップに追加しようとしましたが、別のスレッドにあるため、これを行うことができません。次のエラーが表示されます。
タイプ 'System.IO.FileNotFoundException' の例外が mscorlib.ni.dll で発生し、マネージド/ネイティブ境界の前に処理されませんでした。
タイプ 'System.UnauthorizedAccessException' の初回例外が System.Windows.ni.dll で発生しました
どうすればこれを回避できますか? 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);
}
}
}