using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Net;
using MediaBrowser.UI;
using MediaBrowser.UI.Controls;
using MediaBrowser.UI.ViewModels;
using System;
using System.Windows;
namespace MediaBrowser.Plugins.DefaultTheme.Controls
{
///
/// Interaction logic for BaseItemTile.xaml
///
public partial class HomePageTile : BaseUserControl
{
///
/// Gets the view model.
///
/// The view model.
public DtoBaseItemViewModel ViewModel
{
get { return DataContext as DtoBaseItemViewModel; }
}
///
/// Gets the item.
///
/// The item.
private BaseItemDto Item
{
get { return ViewModel.Item; }
}
///
/// Initializes a new instance of the class.
///
public HomePageTile()
{
InitializeComponent();
DataContextChanged += BaseItemTile_DataContextChanged;
}
///
/// Handles the DataContextChanged event of the BaseItemTile control.
///
/// The source of the event.
/// The instance containing the event data.
void BaseItemTile_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
OnItemChanged();
}
///
/// Called when [item changed].
///
private void OnItemChanged()
{
ReloadImage();
}
///
/// Reloads the image.
///
private void ReloadImage()
{
if (Item.HasPrimaryImage)
{
var url = App.Instance.ApiClient.GetImageUrl(Item, new ImageOptions
{
ImageType = ImageType.Primary,
Height = 225
});
SetImage(url);
}
else if (Item.BackdropCount > 0)
{
var url = App.Instance.ApiClient.GetImageUrl(Item, new ImageOptions
{
ImageType = ImageType.Backdrop,
Height = 225,
Width = 400
});
SetImage(url);
}
else if (Item.HasThumb)
{
var url = App.Instance.ApiClient.GetImageUrl(Item, new ImageOptions
{
ImageType = ImageType.Thumb,
Height = 225,
Width = 400
});
SetImage(url);
}
else
{
SetDefaultImage();
}
}
///
/// Sets the image.
///
/// The URL.
private async void SetImage(string url)
{
try
{
image.Source = await App.Instance.GetRemoteBitmapAsync(url);
}
catch (HttpException)
{
SetDefaultImage();
}
}
private void SetDefaultImage()
{
var imageUri = new Uri("../Resources/Images/VideoDefault.png", UriKind.Relative);
image.Source = App.Instance.GetBitmapImage(imageUri);
}
}
}