CommunityToolkit/Maui.Markup

[Proposal] Add `IImage` Extension Methods

brminnick opened this issue · 4 comments

Add IImage Extension Methods

Link to Discussion

#42

Summary

This Proposal adds the following fluent extension methods for IImage:

public static TBindable Source<TBindable>(this TBindable bindable, ImageSource imageSource) where TBindable : BindableObject, IImageSourcePart;

public static TBindable Aspect<TBindable>(this TBindable bindable, Aspect aspect) where TBindable : BindableObject, IImage;

public static TBindable IsOpaque<TBindable>(this TBindable bindable, bool isOpaque) where TBindable : BindableObject, IImage;

Motivation

Currently, the only way to assign values on an Image is to set the property directly, like so:

var image = new Image { Source = "dotnetbot", IsOpaque = true, Aspect = Aspect.AspectFill };

This Proposal allows us to instead assign these values using the fluent C# Markup syntax.

Detailed Design

public static TBindable Source<TBindable>(this TBindable bindable, ImageSource imageSource) where TBindable : BindableObject, IImageSourcePart
{
	bindable.SetValue(ImageElement.SourceProperty, imageSource);
	return bindable;
}

public static TBindable Aspect<TBindable>(this TBindable bindable, Aspect aspect) where TBindable : BindableObject, IImage
{
	bindable.SetValue(ImageElement.AspectProperty, aspect);
	return bindable;
}

public static TBindable IsOpaque<TBindable>(this TBindable bindable, bool isOpaque) where TBindable : BindableObject, IImage
{
	bindable.SetValue(ImageElement.IsOpaqueProperty, isOpaque);
	return bindable;
}

Usage Syntax

C# Usage

Content = new Image().Source("dotnetbot").Aspect(Aspect.AspectFill).IsOpaque(true);

Drawbacks

No known drawbacks

Alternatives

The current alternative is to assign the properties upon initialization:

var image = new Image { Source = "dotnetbot", IsOpaque = true, Aspect = Aspect.AspectFill };

Unresolved Questions

No unresolved questions

I approve this feature ✅

I also approve ✅

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.