从TimerCallback函数更新UI线程Windows Phone 8

本文关键字:Windows Phone 线程 UI TimerCallback 函数 更新 | 更新日期: 2023-09-27 18:04:49

我有点难住了。

我有一个TimerCallback,每10秒触发一次,其中包含放置在地图上的地理点。我尝试从计时器回调函数内将这些添加到地图中,但由于它在不同的线程中,我无法做到这一点。我收到以下错误:

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

从TimerCallback函数更新UI线程Windows Phone 8

最简单的事情是将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线程的对象。

您也可以尝试使用DispatcherTimer而不是线程。

你还应该注意处理糟糕的网络条件,在这种情况下,一个请求会在超过10秒后超时,你可能会同时运行多个请求——这对你来说可能是一个问题,也可能不是。