Advertisement
Intermediate .NET MAUI, XAML & Markup Extensions

How do you create a custom markup extension?

Short answer

Implement IMarkupExtension (or IMarkupExtension ) with a ProvideValue method returning the computed value, expose properties as inputs, and use it in XAML with {local:MyExtension ...}. Useful for translation lookups, computed values, or DI-resolved values.

Save .NET MAUI mock interview
Advertisement
AModel answer
Implement IMarkupExtension (or IMarkupExtension<T>) with a ProvideValue method returning the computed value, expose properties as inputs, and use it in XAML with {local:MyExtension ...}. Useful for translation lookups, computed values, or DI-resolved values.
public class TranslateExtension : IMarkupExtension<string>
{
    public string Key { get; set; }
    public string ProvideValue(IServiceProvider sp) =>
        AppResources.ResourceManager.GetString(Key) ?? Key;
    object IMarkupExtension.ProvideValue(IServiceProvider sp)
        => ProvideValue(sp);
}
Real-world example
A {local:Translate Key=Welcome} extension localizes text inline from resource files.
Likely follow-ups
  • What method must you implement? | Give a use case.
Related topics