简体   繁体   English

在XAML中使用标记扩展作为属性Setter的值。 {0}对Setter.Value无效。 唯一受支持的MarkupExtension类型是

[英]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 . 下面是一个非常小的示例,说明如何获取此exception This is already a known issue (for more than 7 years!). 这已经是一个已知问题(超过7年!)。 Is there already a solution for that? 已经有解决方案了吗?


Exception 例外

FooExtension is not valid for Setter.Value. FooExtension对于Setter.Value无效。 The only supported MarkupExtension types are 唯一受支持的MarkupExtension类型是
DynamicResourceExtension and BindingBase or derived types. DynamicResourceExtension和BindingBase或派生类型。

C# Class C#类

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. 这不是我正在使用的原始类/ MarkupExtension,但是逻辑(返回SolidColorBrush )是相同的,并且给出了完全相同的错误/行为。

  • Exception during design time 设计时例外
  • Exception in designer gone after rebuild solution 重建解决方案后设计器中的异常消失了
  • No problems in runtime 运行时没有问题

XAML usage XAML用法

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

The extension worked until yesterday for more than 1 year! extension一直工作到昨天超过1年! Including every Windows, VisualStudio or any other update! 包括每个Windows,VisualStudio或任何其他更新! And now since 1 day I got this bug behavior during the design time! 现在从1天开始,我在设计期间就收到了这个错误行为!

Im using: 我正在使用:

  • VS15.7.3 VS15.7.3
  • .NET Framework 4.7.02556 .NET Framework 4.7.02556

Sources: 资料来源:


Binding Workaround (not working) 绑定解决方法(不起作用)

On some pages it is said that you should use binding . 在某些页面上,据说您应该使用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. 我在Storyboard使用了此标记扩展,因此无论如何我都无法返回binding

    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. 我是一种XAML Resources关系。 I defined a ResourceDictionary inside of App.xaml which was already defined in the AssemblyInfo.cs as a XmlnsPrefix / XmlnsDefinition ! 我定义的ResourceDictionaryApp.xaml这是已经在规定AssemblyInfo.csXmlnsPrefix / XmlnsDefinition

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MarkupExtension 对 Setter.Value 无效。 唯一支持的 MarkupExtension 类型是 DynamicResourceExtension 和 BindingBase 或派生类型 - MarkupExtension is not valid for Setter.Value. The only supported MarkupExtension types are DynamicResourceExtension and BindingBase or derived types WPF 我的程序有“MarkupExtension 对 Setter.Value 无效”错误,但在运行时工作正常 - WPF my program has "MarkupExtension is not valid for Setter.Value" error but working fine in runtime 对于使用XAML的Setter,“#333333”不是“ System.Windows.Controls.Border.BorderBrush”属性的有效值 - '#333333' is not a valid value for the 'System.Windows.Controls.Border.BorderBrush' property on a Setter with XAML C#XAML设置器值设置为True,具体取决于字符串属性值 - C# XAML Setter value to True depending on string property value 在Property Setter中,只有在价值不同时设置才有利吗? - In a Property Setter is it Beneficial to only Set if Value Differs? XAML(WPF)属性没有布尔值可访问的设置器 - XAML (WPF) property has no accessible setter to bool value 如何从Xaml中的Style中提取Setter value属性? - How to extract Setter value property from Style in Xaml? 使用PrivateObject设置没有setter的属性值 - Setting property value with no setter using PrivateObject 绑定WPF中的Setter属性的值 - Binding the value of a Setter Property in WPF 属性设置器在XAML中不起作用 - Property setter not working in XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM