public async Task <string> GetLocalImagePath(Uri webUri)
        {
            //Download and save image
            var localUri = await WebDataCache.GetLocalUriAsync(webUri);

            return(localUri.AbsolutePath);
        }
예제 #2
0
        private static async void OnCacheUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //Uri oldCacheUri = (Uri)e.OldValue;
            Uri newCacheUri = (Uri)d.GetValue(CacheUriProperty);
            var image       = (Image)d;

            if (newCacheUri != null)
            {
                try
                {
                    //Get image from cache (download and set in cache if needed)
                    var cacheUri = await WebDataCache.GetLocalUriAsync(newCacheUri);

                    //Set cache uri as source for the image
                    image.Source = new BitmapImage(cacheUri);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);

                    //Revert to using passed URI
                    image.Source = new BitmapImage(newCacheUri);
                }
            }
            else
            {
                image.Source = null;
            }
        }
예제 #3
0
        private async static void ActualImageSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            DelayLoadImage instance = o as DelayLoadImage;

            if (instance == null)
            {
                return;
            }
            instance.imageLoaded = false;

            var newCacheUri = e.NewValue;

            if (newCacheUri == null)
            {
                return;
            }

            var cacheUri =
                await WebDataCache.GetLocalUriAsync(new Uri(newCacheUri.ToString(), UriKind.RelativeOrAbsolute));

            instance._image.UriSource = cacheUri;
            VisualStateManager.GoToState(instance, STATE_DEFAULT_NAME, false);


            //这里引入Q42的缓存
        }
예제 #4
0
        private static async void OnCacheUriChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //Uri oldCacheUri = (Uri)e.OldValue;
            Uri newCacheUri = (Uri)d.GetValue(CacheUriProperty);

            if (newCacheUri != null)
            {
                try
                {
                    //Get image from cache (download and set in cache if needed)
                    var cacheUri = await WebDataCache.GetLocalUriAsync(newCacheUri);

                    // Check if the wanted image uri has not changed while we were loading
                    if (newCacheUri != (Uri)d.GetValue(CacheUriProperty))
                    {
                        return;
                    }

#if NETFX_CORE
                    //Set cache uri as source for the image
                    SetSourceOnObject(d, new BitmapImage(cacheUri));
#elif WINDOWS_PHONE
                    BitmapImage bimg = new BitmapImage();

                    using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (IsolatedStorageFileStream stream = iso.OpenFile(cacheUri.PathAndQuery, FileMode.Open, FileAccess.Read))
                        {
                            bimg.SetSource(stream);
                        }
                    }
                    //Set cache uri as source for the image
                    SetSourceOnObject(d, bimg);
#endif
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);

                    //Revert to using passed URI
                    SetSourceOnObject(d, new BitmapImage(newCacheUri), false);
                }
            }
            else
            {
                SetSourceOnObject(d, null, false);
            }
        }
        public async Task <Uri> GetLocalImageUri(Uri webUri)
        {
            //First delete all old images
            try
            {
                await WebDataCache.ClearAll();
            }
            catch (Exception e)
            {
            }

            //Download and save image
            var localUri = await WebDataCache.GetLocalUriAsync(webUri);

            return(localUri);
        }
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var path = value as string;

            if (String.IsNullOrEmpty(path))
            {
                return(null);
            }
            var imageFileUri = new Uri(path);

            if (imageFileUri.Scheme == "http" || imageFileUri.Scheme == "https")
            {
                return(WebDataCache.GetLocalUriAsync(imageFileUri).Result);
            }

            // 不是网络图片,应用内的素材
            var bm = new BitmapImage(imageFileUri);

            return(bm);
        }