1

这里有点难过。

我有一个每 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);
        }
    }
}
4

1 回答 1

3

最简单的事情是将您的obtainJSON方法中发生的事情包装在一个匿名函数中,该函数被分派回 UI 线程:

public void obtainJSON(Object stateInfo)
{    
    Dispatcher.BeginInvoke(() => { 
        string url = "http://xxx.xxx.xxx" + stateInfo.ToString();
        var client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Decrypt);
        client.DownloadStringAsync(new Uri(url));
    });    
}

这将意味着所有事情(发出 Web 请求和处理响应,因为WebClient现在执行它在调用它的线程上的回调)都发生在 UI 线程上,因此您不会更新从不​​同的 UI 线程绑定到的对象线。

您也可以尝试使用 aDispatcherTimer而不是 threading 。

您还应该注意处理糟糕的网络条件,其中请求将在 10 秒以上后超时,并且您最终可能会运行多个请求 - 这对您来说可能是也可能不是问题。

于 2013-03-20T17:36:54.463 回答