public object this[int i]
        {
            get
            {
                if (_dummyItem == i)
                    return null;

                var tile = _tileSource.TileFromIndex(i);
                var tileId = tile.ToString();

                //if (_tileCache.ContainsKey(tileId))
                //    return new VisualTile(tile, _tileSource, _tileCache[tileId]);

                var tileVm = new VisualTile(tile, _tileSource);

                var imageSource = _tileSource.GetTileLayers(tile.Level, tile.Column, tile.Row);

                var uri = imageSource as Uri;
                if (uri != null)
                {
                    // Capture closure
                    var source = ImageLoader.LoadImage(uri);
                    if (source != null)
                    {
                        source = CacheTile(tileId, source);
                        tileVm.Source = source;
                    }
                    /*var token = _currentCancellationTokenSource.Token;
                    Task.Factory
                        .StartNew(() =>
                        {
                            var source = ImageLoader.LoadImage(uri);
                            if (source != null)
                                source = CacheTile(tileId, source);
                            return source;
                        }, token, TaskCreationOptions.None, TaskScheduler.Default)
                        .ContinueWith(t =>
                        {
                            if (t.Result != null)
                            {
                                tileVm.Source = t.Result;
                            }
                        }, TaskContinuationOptions.OnlyOnRanToCompletion);*/
                }
                else
                {
                    var stream = imageSource as Stream;
                    if (stream != null)
                    {
                        var source = new BitmapImage();
                        source.BeginInit();
                        source.CacheOption = BitmapCacheOption.OnLoad;
                        source.StreamSource = stream;
                        source.EndInit();

                        var src = CacheTile(tileId,(source));
                        tileVm.Source = src;
                    }
                    else return null;
                }

                return tileVm;
            }
            set { }
        }
Beispiel #2
0
        public object this[int i]
        {
            get
            {
                var tile   = _tileSource.TileFromIndex(i);
                var tileId = tile.ToString();

                if (_tileCache.ContainsKey(tileId))
                {
                    return(new VisualTile(tile, _tileSource, _tileCache[tileId]));
                }

                var tileVm = new VisualTile(tile, _tileSource);

                var imageSource = _tileSource.GetTileLayers(tile.Level, tile.Column, tile.Row);

                var uri = imageSource as Uri;
                if (uri != null)
                {
                    // Capture closure
                    var token = _currentCancellationTokenSource.Token;
                    Task.Factory
                    .StartNew(() =>
                    {
                        var source = ImageLoader.LoadImage(uri);
                        if (source != null)
                        {
                            source = CacheTile(tileId, source);
                        }
                        return(source);
                    }, token, TaskCreationOptions.None, TaskScheduler.Default)
                    .ContinueWith(t =>
                    {
                        if (t.Result != null)
                        {
                            tileVm.Source = t.Result;
                        }
                    }, TaskContinuationOptions.OnlyOnRanToCompletion);
                }
                else
                {
                    var stream = imageSource as Stream;
                    if (stream != null)
                    {
                        var source = new BitmapImage();
                        source.BeginInit();
                        source.CacheOption  = BitmapCacheOption.OnLoad;
                        source.StreamSource = stream;
                        source.EndInit();

                        var src = CacheTile(tileId, source);
                        tileVm.Source = src;
                    }
                    else
                    {
                        return(null);
                    }
                }

                return(tileVm);
            }
            set { }
        }