Пример #1
0
        private async Task getImageDataAsync(ImageModel image)
        {
            var url = string.Format(_imageDownloadUrl, image.Image.URL);
            //var bytes = await _httpClient.GetByteArrayAsync(url);
            //image.Data = bytes;
            //var msg = new ImageDownloadCompletedMessage(this, image);
            //_messenger.Publish(msg);

            var wc = new WebClient();
            var tcs = new TaskCompletionSource<byte[]>();
            wc.DownloadDataCompleted += (sender, args) => tcs.SetResult(args.Result);
            wc.DownloadDataAsync(new Uri(url));
            var data = await tcs.Task;
            image.Data = data;
        }
Пример #2
0
        public async void Load(int start, int count)
        {
            Loading = true;
            LoadingComplete = false;

            // now load the stream xml from bing.com
            var query = string.Format(_imageFeed, start, count);
            var data = await getStringAsync(query);

            try
            {
                var xs = new XmlSerializer(typeof(ImageCollection));
                var images = xs.Deserialize(new StringReader(data)) as ImageCollection;

                var currentImages = new List<ImageModel>();

                foreach (var i in images.Images)
                {
                    var url = string.Format(_imageDownloadUrl, i.URL);
                    ImageModel image = null;
                    if (!_images.TryGetValue(url, out image))
                    {
                        image = new ImageModel()
                        {
                            Image = i,
                            URL = url,
                            Description = i.Copyright
                        };
                        _images[url] = image;

                    }
                    var saved = _favoritedImages.Values.FirstOrDefault(si => si.URL == url);
                    if (saved != null)
                    {
                        image.Data = saved.Data;
                    }
                    else getImageDataAsync(image);

                    currentImages.Add(image);
                }

                Images = currentImages;

                /*
                var msg = new AddedImageInfoMessage(this, start, count, currentImages);
                _messenger.Publish(msg);
                 * */
            }
            catch (Exception ex)
            {
            }

            Loading = false;
            LoadingComplete = true;
            if (OnLoadingComplete != null) OnLoadingComplete();
        }