none
Load MapIcon on background thread while startup RRS feed

  • Question

  • How to load MapIcon on background thread while startup? I tried simple code like this but it is not working (mapIcons isn't visible). But when I load mapIcons on foreground (GUI), mapIcons is visible.

    I used VS2015 C# Universal Apps. Thank You.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Windows.Devices.Geolocation;
    using Windows.Foundation;
    using Windows.System;
    using Windows.UI;
    using Windows.UI.Core;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Automation;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Maps;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media.Animation;
    using Windows.UI.Xaml.Navigation;
    using Windows.Storage.Streams;
    using System.ComponentModel;
    
    namespace test 
    {
        public sealed partial class MainPage : Page
        {
            List<MapIcon> _mapIconList = null;
            BackgroundWorker _worker = null;
    
            public MainPage()
            {
                this.InitializeComponent();
    
                mapControl.Loaded += mapControl_Loaded;
            
                _mapIconList = new List<MapIcon>();
                _mapIconList.Clear();
    
                _worker = new BackgroundWorker();
                _worker.DoWork += _worker_DoWork;
                _worker.RunWorkerAsync();
            }
    
            private void mapControl_Loaded(object sender, RoutedEventArgs e)
            {	  
                // initialize map  
                this.InitializeMap(MapStyle.Road);
    
    	    // add mapIcons from collections to mapcontrol
                foreach (var item in _mapIconList)
                {
                    mapControl.MapElements.Add(item);
                }
            }
    	
    	//  initialize map
            private void InitializeMap(MapStyle mapStyle)
            {
                var geoposition = new BasicGeoposition() { Latitude = 12.423105, Longitude = 29.825809, Altitude = 10.0 };
                var geopoint = new Geopoint(geoposition);
                mapControl.Center = geopoint;
                mapControl.Style = mapStyle;
            }
    
    	// do work on background
            private void _worker_DoWork(object sender, DoWorkEventArgs e)
            {
                this.GenerateMapIcons();
            }
    
    	// generate mapIcons and store to collections
            private void GenerateMapIcons()
            {
                Random rand = new Random(10);
                for (int i = 0; i < 100; i++) 
                {
                    var mapIcon = new MapIcon();
    
                    var geoposition = new BasicGeoposition();
                    geoposition.Latitude = mapControl.Center.Position.Latitude + rand.NextDouble();
                    geoposition.Longitude = mapControl.Center.Position.Longitude + rand.NextDouble();
                    geoposition.Altitude = mapControl.Center.Position.Altitude;
    
                    var geopoint = new Geopoint(geoposition);
    
                    mapIcon.Location = geopoint;
                    mapIcon.Title = i.ToString();
                    mapIcon.NormalizedAnchorPoint = new Point(0.5, 1.0);
                    mapIcon.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/mapIcon.png"));
                    mapIcon.CollisionBehaviorDesired = MapElementCollisionBehavior.Hide;
    
                    _mapIconList.Add(mapIcon);            
                }
            }
    
        }
    }
    
    


    • Edited by mb,, Monday, November 16, 2015 7:19 AM edit code
    Monday, November 16, 2015 7:17 AM

Answers

  • A MapIcon inherits form the DependencyObject class. Any class that inherits from this class cannot be created in the background, they need to be created on the UI thread.

    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Monday, November 16, 2015 5:59 PM
    • Marked as answer by mb,, Thursday, November 19, 2015 1:40 PM
    Monday, November 16, 2015 5:59 PM