Пример #1
0
        private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (IsoStoreImageSource._cacheDict == null)
            {
                IsoStoreImageSource._cacheDict = new Dictionary <string, byte[]>();
            }
            Image img = d as Image;

            if (img == null)
            {
                return;
            }
            string path = e.NewValue as string;

            if (string.IsNullOrEmpty(path))
            {
                img.Source = null;
            }
            else if (IsoStoreImageSource._cacheDict.ContainsKey(path))
            {
                MemoryStream memoryStream = new MemoryStream(IsoStoreImageSource._cacheDict[path]);
                IsoStoreImageSource.SetSource(img, (Stream)memoryStream);
                memoryStream.Close();
            }
            else
            {
                SynchronizationContext uiThread = SynchronizationContext.Current;
                Task.Factory.StartNew((Action)(() =>
                {
                    try
                    {
                        using (IsolatedStorageFile storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            if (!storeForApplication.FileExists(path))
                            {
                                return;
                            }
                            IsolatedStorageFileStream stream = storeForApplication.OpenFile(path, FileMode.Open, FileAccess.Read);
                            uiThread.Post((SendOrPostCallback)(_ =>
                            {
                                if (IsoStoreImageSource.CurrentCacheSize < IsoStoreImageSource.MAX_CACHE_SIZE)
                                {
                                    IsoStoreImageSource._cacheDict[path] = StreamUtils.ReadFullyToByteArray((Stream)stream);
                                    IsoStoreImageSource.CurrentCacheSize += IsoStoreImageSource._cacheDict[path].Length;
                                }
                                stream.Position = 0L;
                                if (IsoStoreImageSource.GetIsoStoreFileName((UIElement)img) == path)
                                {
                                    IsoStoreImageSource.SetSource(img, (Stream)stream);
                                }
                                stream.Close();
                            }), null);
                        }
                    }
                    catch
                    {
                    }
                }));
            }
        }