简体   繁体   中英

Using a Markup Extension as value of property Setter in XAML. {0} is not valid for Setter.Value. The only supported MarkupExtension types are

Below is a very small example how to get this exception . This is already a known issue (for more than 7 years!). Is there already a solution for that?


Exception

FooExtension is not valid for Setter.Value. The only supported MarkupExtension types are
DynamicResourceExtension and BindingBase or derived types.

C# Class

public class FooExtension : MarkupExtension
{
    public string Key { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return new SolidColorBrush((Color)ColorConverter.ConvertFromString(Key));
    }
}

This is not the original class/MarkupExtension I'm using, but the logic (return SolidColorBrush ) are identical and it gives exactly the same error/behavior.

  • Exception during design time
  • Exception in designer gone after rebuild solution
  • No problems in runtime

XAML usage

Background="{extensions:Foo Key=#FFF37C21}"

The extension worked until yesterday for more than 1 year! Including every Windows, VisualStudio or any other update! And now since 1 day I got this bug behavior during the design time!

Im using:

  • VS15.7.3
  • .NET Framework 4.7.02556

Sources:


Binding Workaround (not working)

On some pages it is said that you should use binding . But there I got the same error/behavior!

btw. I use this markup extension in a Storyboard , so I can't return a binding anyway.

    public override object ProvideValue(IServiceProvider serviceProvider)
    {            
        Binding binding = new Binding();
        binding.Source = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Key));

        return binding.ProvideValue(serviceProvider);
    }

I solved the problem! I was a kind of XAML Resources relationship. I defined a ResourceDictionary inside of App.xaml which was already defined in the AssemblyInfo.cs as a XmlnsPrefix / XmlnsDefinition !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM