0
0
forked from s434786/DINO_SCRUM

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.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));
}
}
}

View File

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

View File

@ -20,6 +20,7 @@ using Newtonsoft.Json.Linq;
using System.Net;
using Magazyn.Windows;
using Magazyn.Tools;
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;
}
}
}
}