예제 #1
0
        public DataExampleViewModel()
        {
            StartLongRunningDataLoader = new DataLoader();
            StartLongRunningWithExceptionDataLoader = new DataLoader(); //swallow exceptions by default
            CacheDataLoader = new DataLoader();
            CacheWithExceptionDataLoader = new DataLoader();            //swallow exceptions by default
            SourceABDataLoader           = new DataLoader();            //swallow exceptions by default
            CacheRefreshDataLoader       = new DataLoader();            //swallow exceptions by default
            FailCacheDataLoader          = new DataLoader();
            FailCacheSuccessDataLoader   = new DataLoader();


            StartLongRunningCommand = new RelayCommand(() => StartLongRunningAction());
            StartLongRunningWithExceptionCommand = new RelayCommand(() => StartLongRunningWithExceptionAction());
            CacheCommand = new RelayCommand(() => CacheAction());
            CacheWithExceptionCommand = new RelayCommand(() => CacheWithExceptionAction());
            SourceABCommand           = new RelayCommand(() => SourceABAction());
            CacheRefreshCommand       = new RelayCommand(() => CacheRefreshAction());
            ClearCacheCommand         = new RelayCommand(() => ClearCacheAction());
            FailCacheCommand          = new RelayCommand(() => FailCacheAction());
            FailCacheSuccessCommand   = new RelayCommand(() => FailCacheSuccessAction());

            ClearWebDataCacheCommand = new RelayCommand(() => ClearWebDataCacheCommandAction());
            GetUriCommand            = new RelayCommand(() => GetUriCommandAction());

            ImageBrushUri = new Uri("https://pbs.twimg.com/profile_images/478304416148099072/1_rxoQgR.png");

            WebDataCache.Init();
        }
예제 #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;
            }
        }
        public async Task <string> GetLocalImagePath(Uri webUri)
        {
            //Download and save image
            var localUri = await WebDataCache.GetLocalUriAsync(webUri);

            return(localUri.AbsolutePath);
        }
예제 #4
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的缓存
        }
예제 #5
0
 // Code to execute when the application is activated (brought to foreground)
 // This code will not execute when the application is first launched
 private async void Application_Activated(object sender, ActivatedEventArgs e)
 {
     if (!e.IsApplicationInstancePreserved)
     {
         await WebDataCache.Init();
     }
 }
예제 #6
0
        /// <summary>
        /// Load uri and set data in cache
        /// </summary>
        private async void GetUriCommandAction()
        {
            var result = await WebDataCache.GetAsync(new Uri("http://microsoft.com"), forceGet : false);

            if (result == null)
            {
            }
        }
예제 #7
0
        public async Task GetAsyncLongUriTest()
        {
            int length = 500;

            var baseUri = "http://www.google.com/favicon.ico?";
            var q       = new string('x', length - baseUri.Length);

            await WebDataCache.GetAsync(new Uri(baseUri + q), true);
        }
예제 #8
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);
        }
예제 #10
0
        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);
        }
예제 #11
0
        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used when the application is launched to open a specific file, to display
        /// search results, and so forth.
        /// </summary>
        /// <param name="args">Details about the launch request and process.</param>
        protected override async void OnLaunched(LaunchActivatedEventArgs args)
        {
            await WebDataCache.Init();

            // Do not repeat app initialization when already running, just ensure that
            // the window is active
            if (args.PreviousExecutionState == ApplicationExecutionState.Running)
            {
                Window.Current.Activate();
                return;
            }

            // Create a Frame to act as the navigation context and associate it with
            // a SuspensionManager key
            var rootFrame = new Frame();

            SuspensionManager.RegisterFrame(rootFrame, "AppFrame");

            if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
            {
                // Restore the saved session state only when appropriate
                await SuspensionManager.RestoreAsync();
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                if (!rootFrame.Navigate(typeof(MainPage)))
                {
                    throw new Exception("Failed to create initial page");
                }
            }

            // Place the frame in the current Window and ensure that it is active
            Window.Current.Content = rootFrame;
            Window.Current.Activate();
        }
예제 #12
0
        public IllustrationViewModel(Uri imgSourceUri)
        {
            Content           = null;
            IsLoading         = true;
            NonCachedImageUri = imgSourceUri;
            var cacheingFileTask = WebDataCache.GetAsync(imgSourceUri);

            cacheingFileTask.ContinueWith(localUri =>
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (localUri.Status != TaskStatus.RanToCompletion)
                    {
                        Content = imgSourceUri.AbsolutePath;
                    }
                    else
                    {
                        Content = localUri.Result.Path;
                    }
                    IsLoading = false;
                });
            });
        }
예제 #13
0
 public async Task Init()
 {
     await WebDataCache.Init();
 }
예제 #14
0
 // Code to execute when the application is launching (eg, from Start)
 // This code will not execute when the application is reactivated
 private async void Application_Launching(object sender, LaunchingEventArgs e)
 {
     await WebDataCache.Init();
 }
예제 #15
0
 public static async void InitNavigationConfigurationInThisAssembly()
 {
     StartupFunctions.RunAllConfig();
     await WebDataCache.Init();
 }
예제 #16
0
 private void DeleteButton_Click(object sender, RoutedEventArgs e)
 {
     WebDataCache.Delete(new Uri("https://www.google.com/images/logo.png"));
 }
예제 #17
0
 /// <summary>
 /// Clear WebDataCache
 /// </summary>
 private void ClearWebDataCacheCommandAction()
 {
     WebDataCache.ClearAll();
 }
예제 #18
0
 public async Task GetAsyncTest()
 {
     await WebDataCache.GetAsync(new Uri("http://www.google.com/favicon.ico"), true);
 }