1

ここで少し困惑。

マップに配置するジオポイントを含む 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);
        }
    }
}
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スレッドからUIスレッドにバインドされているオブジェクトを更新しないことを意味します。スレッド。

DispatcherTimerスレッドではなく、を使用してみることもできます。

また、リクエストが10秒を超えるとタイムアウトになり、複数のリクエストが実行される可能性がある、ネットワークの状態が悪い場合の処理​​にも注意する必要があります。これは、問題になる場合と問題にならない場合があります。

于 2013-03-20T17:36:54.463 に答える