Reputation: 8375
I created an ImageResourceExtension class in a under Common Assembly name. I checked the name under the Reference in Property window setting. Namespace for this extension is Extension.Common.
The xaml of the working project which using this Common Assembly have a declaration as:
xmlns:common="clr-namespace:Extension.Common;assembly:Common"
The element was written as :
<common:BooleanToObjectConverter x:Key="boolToStyleImage"
x:TypeArguments="Style">
<common:BooleanToObjectConverter.FalseObject>
<Style TargetType="Image">
<Setter Property="HeightRequest" Value="20" />
<Setter Property="Source" Value="{common:ImageResource Common.Images.error.png}" />
</Style>
</common:BooleanToObjectConverter.FalseObject>
The images was stored in a Common project folder inside Images directory
I named the ImageResource file as ImageResourceExtension.cs and tried ImageResource.cs but none of it work. It just give me an exception saying:
MarkupExtension not found for common:ImageResource
I have no idea what go wrong. Have follow all the steps for a blog.
The project that I placed those xaml stuff are in a shared project. Don't know will it make any difference
Upvotes: 0
Views: 258
Reputation: 3964
I hope I understand good your question !
look at this code:
[ContentProperty("Source")]
public class ImageSourceExtension : IMarkupExtension
{
public string Source { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Source == null)
{
return null;
}
// Do your translation lookup here, using whatever method you require
var imageSource = ImageSource.FromResource("Project.Images." + Source);
return imageSource;
}
}
Now, if you want to use an image as you did in your xaml, something like that should work:
<Image Source="{extension:ImageSource LogoProject.png}"/>
Note 1: I only put the name of the image because in the extension, the path is put automatically ;)
Note 2: You have to put the assembly/namespace as I do below:
xmlns:extension="clr-namespace:Project.Sources.Extensions;assembly=Project"
Also, you're image has to be an EmbeddedResource, do not forget about it. For the path, it's Project.Path.Image.png
and not assembly as well ;)
I hope I understood your question and I could help you :)
Upvotes: 2