Added caching class to handle images

This commit is contained in:
Konrad Pierzyński 2019-01-23 23:10:44 +01:00
parent 1465eead02
commit c73fd4e058
4 changed files with 37 additions and 7 deletions

View File

@ -1,8 +1,10 @@
using System; using Magazyn.Tools;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging; using System.Windows.Media.Imaging;
namespace Magazyn.DataModels namespace Magazyn.DataModels
@ -79,6 +81,7 @@ namespace Magazyn.DataModels
public Fruit(int id, string name, int quantity, int quantityMax, float price, string imageLink) public Fruit(int id, string name, int quantity, int quantityMax, float price, string imageLink)
{ {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.quantity = quantity; this.quantity = quantity;
@ -86,11 +89,7 @@ namespace Magazyn.DataModels
this.price = price; this.price = price;
this.imageLink = imageLink; this.imageLink = imageLink;
imageSource = new BitmapImage(); imageSource = CacheImage.GetImageSource(new Uri(imageLink));
imageSource.BeginInit();
imageSource.UriSource = new Uri(imageLink);
imageSource.CacheOption = BitmapCacheOption.OnLoad;
imageSource.EndInit();
} }
} }
} }

View File

@ -67,6 +67,7 @@
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="DataModels\Fruit.cs" /> <Compile Include="DataModels\Fruit.cs" />
<Compile Include="DataModels\WarehousePrice.cs" /> <Compile Include="DataModels\WarehousePrice.cs" />
<Compile Include="Tools\CacheImage.cs" />
<Compile Include="Views\FruitView.xaml.cs"> <Compile Include="Views\FruitView.xaml.cs">
<DependentUpon>FruitView.xaml</DependentUpon> <DependentUpon>FruitView.xaml</DependentUpon>
</Compile> </Compile>

View File

@ -20,6 +20,7 @@ using Newtonsoft.Json.Linq;
using System.Net; using System.Net;
using Magazyn.Windows; using Magazyn.Windows;
using Magazyn.Tools;
namespace Magazyn namespace Magazyn
{ {

View File

@ -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<Uri, BitmapImage> dic = new Dictionary<Uri, BitmapImage>();
static public BitmapImage GetImageSource( Uri url )
{
if (dic.Keys.Contains<Uri>(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;
}
}
}
}