首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >MarkupExtension对Setter.Value无效。唯一受支持的MarkupExtension类型是DynamicResourceExtension和BindingBase或派生类型。

MarkupExtension对Setter.Value无效。唯一受支持的MarkupExtension类型是DynamicResourceExtension和BindingBase或派生类型。
EN

Stack Overflow用户
提问于 2018-10-23 02:23:42
回答 1查看 1.3K关注 0票数 6

我有一个定制的标记扩展"ThemeExtension“来提供来自我的DefaultTheme.xaml ResourceDictionary的"SolidColorBrush”。

调用示例:BorderBrush="{extensions:Theme Key= FooKeyValue}"

它在运行时运行时没有任何问题,但有时它在设计时开始崩溃,我无法再进行开发了。设计师崩溃了。重建,清洁解决方案,操作系统重新启动不再有帮助。如果我在XAML代码中更改了一些值,它就是为1张绘图而工作的!在那之后它又坠毁了!

预览

XAML Stacktrace

代码语言:javascript
运行
AI代码解释
复制
bei System.Windows.Setter.Seal()
bei System.Windows.SetterBaseCollection.Seal()
bei System.Windows.Style.Seal()
bei System.Windows.StyleHelper.UpdateStyleCache(FrameworkElement fe, FrameworkContentElement fce, Style oldStyle, Style newStyle, Style& styleCache)
bei System.Windows.FrameworkElement.OnStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
bei System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
bei System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
bei System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
bei System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
bei System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
bei System.Windows.FrameworkElement.UpdateStyleProperty()
bei System.Windows.FrameworkElement.OnInitialized(EventArgs e)
bei System.Windows.Controls.Primitives.Selector.OnInitialized(EventArgs e)
bei System.Windows.FrameworkElement.TryFireInitialized()
bei System.Windows.FrameworkElement.EndInit()
bei System.Windows.Controls.ItemsControl.EndInit()
bei MS.Internal.Xaml.Runtime.ClrObjectRuntime.InitializationGuard(XamlType xamlType, Object obj, Boolean begin)

ThemeExtension.cs

代码语言:javascript
运行
AI代码解释
复制
[MarkupExtensionReturnType(typeof(Color))]
public class ThemeColorExtension : ThemeExtension
{
    internal override object ModifyThemeValue(object value)
    {
        if (value is SolidColorBrush solidColorBrush)
            return solidColorBrush.Color;
        return value;
    }
}

[MarkupExtensionReturnType(typeof(SolidColorBrush))]
public class ThemeExtension : MarkupExtension
{
    // ##############################################################################################################################
    // Properties
    // ##############################################################################################################################

    #region Properties

    // ##########################################################################################
    // Public Properties
    // ##########################################################################################

    /// <summary>
    /// The Key in the Resource Theme file
    /// </summary>
    public string Key { get; set; }

    // ##########################################################################################
    // Private Properties
    // ##########################################################################################

    private static readonly List<ThemeExtension> _Cache = new List<ThemeExtension>();
    private static readonly ResourceDictionary _DefaultTheme;
    private static ResourceDictionary _CurrentTheme;

    private PropertyInfo _Property { get; set; }
    private DependencyProperty _DependencyProperty { get; set; }
    private WeakReference _TargetReference { get; set; }

    #endregion

    // ##############################################################################################################################
    // Constructor
    // ##############################################################################################################################

    #region Constructor

    static ThemeExtension()
    {
        _DefaultTheme = new ResourceDictionary
        {
            Source = new Uri("/HtPlcFramework;component/Themes/DefaultTheme.xaml", UriKind.Relative)
        };
        _CurrentTheme = _DefaultTheme;

        NavigationService.Navigated += _OnNavigated;
    }

    public ThemeExtension() { }

    #endregion

    // ##############################################################################################################################
    // public methods
    // ##############################################################################################################################

    #region public methods

    /// <summary>
    /// https://social.msdn.microsoft.com/Forums/vstudio/en-US/931d7bff-90b6-4a70-bb0b-3a097e1301a1/net-40-breaking-change-using-a-markup-extension-as-value-of-property-setter-in-xaml-style?forum=wpf
    /// </summary>
    /// <param name="serviceProvider"></param>
    /// <returns></returns>
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
        if (target == null)
            return this;

        if (target.TargetObject != null && target.TargetProperty != null)
        {
            _TargetReference = new WeakReference(target.TargetObject);
            if (target.TargetProperty.GetType() == typeof(PropertyInfo))
            {
                _Property = (PropertyInfo)target.TargetProperty;
            }
            else if (target.TargetProperty is DependencyProperty)
            {
                _DependencyProperty = (DependencyProperty)target.TargetProperty;
            }
        }

        if (!_Cache.Contains(this))
            _Cache.Add(this);

        return ModifyThemeValue(_ReadThemeKey(Key));
    }

    /// <summary>
    /// Change the Theme set
    /// </summary>
    /// <param name="themeUri">Default is: new Uri("/HtPlcFramework;component/Themes/DefaultTheme.xaml", UriKind.Relative)</param>
    public static void ChangeTheme(Uri themeUri)
    {
        _CurrentTheme = new ResourceDictionary { Source = themeUri };

        foreach (ThemeExtension reference in _Cache)
        {
            reference._UpdateTheme();
        }
    }

    /// <summary>
    /// Get the current theme entry. Can be null!
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public static object ReadThemeKey(string key) => _ReadThemeKey(key);

    internal virtual object ModifyThemeValue(object value)
    {
        return value;
    }

    #endregion

    // ##############################################################################################################################
    // private methods
    // ##############################################################################################################################

    #region private methods

    private static void _OnNavigated(object sender, string layer)
    {
        _Cache.RemoveAll(ti => !ti._TargetReference.IsAlive);
    }

    private static object _ReadThemeKey(string key)
    {
        try
        {
            return _CurrentTheme[key] ?? _DefaultTheme[key];
        }
        catch (Exception)
        {
            Trace.WriteLine($"The key '{key}' was not found in {_CurrentTheme.Source}!");
            return null;
        }
    }

    private void _UpdateTheme()
    {
        if (_TargetReference.IsAlive)
        {
            if (_Property != null)
                _Property.GetSetMethod().Invoke(_TargetReference.Target, new object[] { _ReadThemeKey(Key) });
            else if (_DependencyProperty != null)
            {
                DependencyObject dependencyObject = _TargetReference.Target as DependencyObject;
                dependencyObject?.SetValue(_DependencyProperty, _ReadThemeKey(Key));
            }
        }
        else
        {
            _Cache.Remove(this);
        }
    }

    #endregion

} 

相关的VStudio开发者社区帖子

https://developercommunity.visualstudio.com/content/problem/364029/foomarkupextension-is-not-valid-for-settervalue-th.html

相关职位无解决办法

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

VS2010 Custom MarkupExtension

在以下网站上有一个“标记扩展”的例子:

https://dzone.com/articles/extend-wpf-add-your-own-keywor (下载项目示例http://raasiel.typepad.com/MyXamlExtensions.zip)

如果我运行这个例子,我也会得到这个异常!因此,也许这是VisualStudio的一个问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-28 00:11:40

感谢Michael,因为他为我指明了正确的方向

如您所见(https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Setter.cs,123),在Setter中只允许DynamicResourceExtension作为MarkupExtension

因此,为了处理这个*错误消息:

  1. 将基类MarkupExtension更改为DynamicResourceExtension
  2. 提供一个foo资源密钥(只是为了绕过ArgumentNullException;参见https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/DynamicResourceExtension.cs,43)
代码语言:javascript
运行
AI代码解释
复制
[MarkupExtensionReturnType(typeof(SolidColorBrush))]
public class ThemeExtension : DynamicResourceExtension
{

...

    public ThemeExtension() : base("foo")
    {
    }

...

}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52946703

复制
相关文章
关联容器
之前介绍过标准库中的顺序容器,顺序容器是元素在内存中按照一定顺序进行排列的,都是按线性结构进行排列。除了顺序容器外,c++中还有关联容器。与顺序容器不同的是,关联容器中元素是按照关键字来保存和访问的。与之相对的顺序容器是按它们在容器中的位置来顺序的保存和访问的。
Masimaro
2021/05/18
7260
关联容器小结
关联容器和顺序容器的根本不同之处在于,关联容器中的元素是按关键字来保存和访问的(比如map和set),而顺序容器中的元素是按照在容器中的位置来顺序保存和访问的(比如vector和string)。
Enterprise_
2019/11/12
4800
画出最佳分组的生存曲线
做生存分析,Best separation和Median separation,后者很简单,很想学前者,这次带来的是最佳分组的曲线代码。
生信喵实验柴
2023/09/06
3600
画出最佳分组的生存曲线
第 11 章 关联容器
第 11 章 关联容器 标签: C++Primer 学习记录 关联容器 ---- 第 11 章 关联容器 11.1 使用关联容器 11.2 关联容器概述 11.3 关联容器操作 11.4 无序容器 ---- 11.1 使用关联容器 标准库中定义了 8个关联容器,这些容器的不同体现在三个维度上。 或者是一个 set,或者是一个 map。 或者要求不重复的关键字,或者允许重复关键字,允许重复的容器的名字中都包含单词 multi。 或者按顺序保存元素或无序保存。不保持关键字按顺序存储的容器的名字都以 unord
用户1653704
2018/06/07
5680
关联式容器set和map
序列式容器:vector,list,deque,forward_lsit都是序列式容器,因为它们的底层都是线性序列的数据结构,存放的是元素本身。
始终学不会
2023/10/17
2250
关联式容器set和map
【STL】关联容器 — hash_set
容器hash_set是以hash table为底层机制的,差点儿所有的操作都是转调用hash table提供的接口。因为插入无法存储同样的键值,所以hash_set的插入操作所有都使用hash table的insert_unique接口,代码例如以下:
全栈程序员站长
2022/07/13
1880
【STL】关联容器 — hash_set
[入门] Docker将nginx容器和php容器关联起来
首先是在菜鸟教程里看的教程,里面把各种镜像、容器的概念和基本操作都说了。但是每一步都直到怎么测试运行起来。
宣言言言
2019/12/15
3.2K0
STL关联容器-红黑树
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haluoluo211/article/details/80877064
bear_fish
2018/09/14
5460
STL关联容器-红黑树
寻找生存分析的最佳基因表达分组阈值
文字版是: Based on the FPKM value of each gene, we classified the patients into two groups and examined
生信技能树
2019/05/17
1.5K0
规模化运行容器时的最佳数据存储路径
K8s和其他容器编排平台正在迅速下沉到主流的基础设置中,对于大多数面向业务的应用,从传统的数据中心迁移到容器部署还算独立和简单。然而,当遇到需要像数据库或快速数据分析工作负载这样要求更高的核心应用时,事情不那么简单了。
灵雀云
2022/06/06
5700
规模化运行容器时的最佳数据存储路径
数据分组
数据分组就是根据一个或多个键(可以是函数、数组或df列名)将数据分成若干组,然后对分组后的数据分别进行汇总计算,并将汇总计算后的结果合并,被用作汇总计算的函数称为就聚合函数。 Python中对数据分组利用的是 groupby() 方法,类似于sql中的 groupby。 1.分组键是列名 分组键是列名时直接将某一列或多列的列名传给 groupby() 方法,groupby() 方法就会按照这一列或多列进行分组。 groupby(): """ 功能: 根据分组键将数据分成
见贤思齊
2020/08/05
4.6K0
STL之关联式容器map(一)
map<K,T> 类模板:定义了一个保存 T 类型对象的 map,每个 T 类型的对象都有一个关联的 K 类型的键。容器内对象的位置是通过比较键决定的,唯一的要求是键必须可以用 less<K> 比或用自己指定的另一个函数对象来替代。
用户9831583
2022/06/16
3880
STL之关联式容器map(一)
C++primer笔记之关联容器
在这一章中,有以下的几点收获: 1、pair类型的使用相当频繁,如果需要定义多个相同的pair类型对象,可考虑利用typedef简化其声明: typedef pair<string, string> A;这样,在后面的使用中就可以直接用A来代替前面繁琐的书写。 2、三种方法创建pair对象: (1)第一种方法:使用函数make_pair() pair<string, string> spair; string first, last; while(cin >> first >> last) {   spai
Linux云计算网络
2018/01/10
6920
C++primer笔记之关联容器
STL之关联式容器map(二)
成员函数 emplace() 和 insert() 返回的 pair 对象提供的指示相同。pair 的成员变量 first 是一个指向插入元素或阻止插入的元素的迭代器;成员变量 second 是个布尔值,如果元素插入成功,second 就为 true。
用户9831583
2022/06/16
5740
STL之关联式容器map(二)
块存储、对象存储、文件存储, 容器存储的最佳方式应该是什么?
容器的无状态临时存储是一个很好的特性。从镜像启动一个容器,修改,停止,然后重新启动一个容器。一个全新的跟镜像一模一样的容器回来了。
焱融科技
2020/02/28
4.6K0
块存储、对象存储、文件存储, 容器存储的最佳方式应该是什么?
VSCode关联Laradock 容器配置PHPCS插件
本文主要记录如何在 VSCode 关联 Laradock 容器,配置和使用容器的 PHP 环境和一些插件,如:phpcs。
coding01
2021/01/18
1.5K0
【说站】mysql分组查询是什么
以上就是mysql分组查询的介绍,希望对大家有所帮助。更多编程基础知识学习:python学习网
很酷的站长
2022/11/23
9760
docker容器入门最佳教程
容器技术是继大数据和云计算之后又一炙手可热的技术,而且未来相当一段时间内都会非常流行。
sunsky
2020/08/20
6910
docker容器入门最佳教程
容器安全最佳实践入门
保证容器安全是一项复杂的任务。这个问题域很广,面对大量的检查清单和最佳实践,你很难确定采用哪个解决方案。所以,如果你要实现容器安全策略,应该从哪里开始呢?
深度学习与Python
2021/01/20
6720
点击加载更多

相似问题

欧几里德距离vs皮尔逊相关性vs余弦相似度?

312

文档间相似性(余弦相似性)

13

文本(余弦)相似性

110

Python,余弦相似与调整余弦相似

11

调整后的余弦相似性不能正常工作

110
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档