using System;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;
namespace OwlCore.WinUI.Converters
{
///
/// A converter that converts a given to a .
///
public sealed class UriToImageSourceConverter : IValueConverter
{
///
/// Converts a or url string to a .
///
/// The uri or url string.
/// A with the url as a source.
public static BitmapImage? Convert(object value)
{
Uri? uri = null;
if (value is Uri)
{
uri = value as Uri;
}
else if (value is string stringUri)
{
Uri.TryCreate(stringUri, UriKind.Absolute, out uri);
}
if (uri != null)
{
return new BitmapImage(uri);
}
return null;
}
///
public object? Convert(object value, Type targetType, object parameter, string language)
{
return Convert(value);
}
///
public object? ConvertBack(object value, Type targetType, object parameter, string language)
{
if (value is BitmapImage bitmap)
{
return bitmap.UriSource;
}
return null;
}
}
}