Set IMarkupExtension parameter in XAML from a fixed list

19 Views Asked by At

I am trying to define a XAML markup extension which accept parameter from a predefined list.

I have created a stripped down version of the extension just to explain my issue. This is how it looks like:

public struct TestKeys
{
    public readonly static TestKeys Key1 = new("One");
    public readonly static TestKeys Key2 = new("Two");
    public readonly static TestKeys Key3 = new("Three");
    public readonly static TestKeys Key4 = new("Four");

    public string Value { get; }

    TestKeys(string name) { Value = name; }
}

public class TestExtension : IMarkupExtension<string?>
{
    public TestKeys Parameter { get; set; }

    public string? ProvideValue(IServiceProvider serviceProvider) =>
        Parameter.Value;

    object? IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) =>
        Parameter.Value;
}

Now when I edit my XAML file, I can see the 4 options: See screenshot

But when I run the app I get an XFC0009 error stating: No property, BindableProperty, or event found for "Parameter", or mismatching type between value and property.

If I use an enum instead of the struct:

public enum TestKeys
{
    One, Two, Three, Four
}

public class TestExtension : IMarkupExtension<string?>
{
    public TestKeys Parameter { get; set; }

    public string? ProvideValue(IServiceProvider serviceProvider) =>
        Parameter.ToString();

    object? IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) =>
        Parameter.ToString();
}

it works just fine! Unfortunately I need something more than a string for my actual extension to work properly.

Is there any way to accomplish what I am after?

Thanks in advance for your help! Maurizio.

0

There are 0 best solutions below