As you guys know, there is no built-in type converter class to convert a string to ImageSource object.
That brough me a challenge when I want to have my models have a string property representing ResouceID for an embeded image and, then, XAML's Image.Source attribute bound to that property by data binding.
The easiest solution is adding another property of ImageSource type in the model which is only getting an image source object generated based on the string ResourceID peroperty and then, binding it to Image.Source.
It works well but the problem is I had to add Xamarin.Forms dependency to the project defining the model which was .NET Stardard class library.
I have split view and model/viewmodel by project so that the model can be reused at other applications. so, the model was designed to have only basic types like int, string, and so on. The easiest solution have made the model class dependent on Xamarin.Forms and I worry if it may bring about some issues later when I use for other frameworks.
In order to make a solution, I tried declaring a markup extension class with binding capability in the shared project by referring to MS documents but not working.
The extension class code is as below.
// This class is added to the shared project, where a page class exsists.
public class BindableImageSourceExtension : BindableObject, IMarkupExtension
{
// This is a unique property of BindableImageSource element
public string ResourceID
{
get { return (string)GetValue(ResouceIDProperty); }
set { SetValue(ResouceIDProperty, value); }
}
public object ProvideValue(IServiceProvider serviceProvider)
{
if(ResourceID == null) return null;
// Images are embeded to a project named BusinessSession that holds Data class.
return ImageSource.FromResource(ResourceID, typeof(BusinessSession).GetTypeInfo().Assembly);
}
public static readonly BindableProperty ResouceIDProperty =
BindableProperty.Create("ResourceID", typeof(string),
typeof(BindableImageSourceExtension),
default(string));
}
Anybody can help this out?