From c73fd4e058ae2bcdbe0e9dc393f7f885716ef46a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Pierzy=C5=84ski?= Date: Wed, 23 Jan 2019 23:10:44 +0100 Subject: [PATCH] Added caching class to handle images --- Magazyn_Client/Magazyn/DataModels/Fruit.cs | 13 +++++----- Magazyn_Client/Magazyn/Magazyn.csproj | 1 + Magazyn_Client/Magazyn/MainWindow.xaml.cs | 1 + Magazyn_Client/Magazyn/Tools/CacheImage.cs | 29 ++++++++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 Magazyn_Client/Magazyn/Tools/CacheImage.cs diff --git a/Magazyn_Client/Magazyn/DataModels/Fruit.cs b/Magazyn_Client/Magazyn/DataModels/Fruit.cs index 47194f4..ad60ffd 100644 --- a/Magazyn_Client/Magazyn/DataModels/Fruit.cs +++ b/Magazyn_Client/Magazyn/DataModels/Fruit.cs @@ -1,8 +1,10 @@ -using System; +using Magazyn.Tools; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows; using System.Windows.Media.Imaging; namespace Magazyn.DataModels @@ -79,6 +81,7 @@ namespace Magazyn.DataModels public Fruit(int id, string name, int quantity, int quantityMax, float price, string imageLink) { + this.id = id; this.name = name; this.quantity = quantity; @@ -86,11 +89,7 @@ namespace Magazyn.DataModels this.price = price; this.imageLink = imageLink; - imageSource = new BitmapImage(); - imageSource.BeginInit(); - imageSource.UriSource = new Uri(imageLink); - imageSource.CacheOption = BitmapCacheOption.OnLoad; - imageSource.EndInit(); + imageSource = CacheImage.GetImageSource(new Uri(imageLink)); } - } + } } diff --git a/Magazyn_Client/Magazyn/Magazyn.csproj b/Magazyn_Client/Magazyn/Magazyn.csproj index 94e47cb..d2a77ae 100644 --- a/Magazyn_Client/Magazyn/Magazyn.csproj +++ b/Magazyn_Client/Magazyn/Magazyn.csproj @@ -67,6 +67,7 @@ + FruitView.xaml diff --git a/Magazyn_Client/Magazyn/MainWindow.xaml.cs b/Magazyn_Client/Magazyn/MainWindow.xaml.cs index 6902005..7d09588 100644 --- a/Magazyn_Client/Magazyn/MainWindow.xaml.cs +++ b/Magazyn_Client/Magazyn/MainWindow.xaml.cs @@ -20,6 +20,7 @@ using Newtonsoft.Json.Linq; using System.Net; using Magazyn.Windows; +using Magazyn.Tools; namespace Magazyn { diff --git a/Magazyn_Client/Magazyn/Tools/CacheImage.cs b/Magazyn_Client/Magazyn/Tools/CacheImage.cs new file mode 100644 index 0000000..309b869 --- /dev/null +++ b/Magazyn_Client/Magazyn/Tools/CacheImage.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Media.Imaging; + +namespace Magazyn.Tools +{ + class CacheImage + { + static Dictionary dic = new Dictionary(); + + static public BitmapImage GetImageSource( Uri url ) + { + if (dic.Keys.Contains(url)) return dic[url]; + else + { + BitmapImage imageSource = new BitmapImage(); + imageSource.BeginInit(); + imageSource.UriSource = url; + imageSource.CacheOption = BitmapCacheOption.OnLoad; + imageSource.EndInit(); + dic.Add(url, imageSource); + return imageSource; + } + } + } +}