c# - 在 .NET 中使用 Ambient 属性?

标签 c# xaml serialization c#-4.0

有人可以阐明 .NET 中的环境属性吗?

最佳答案

用于解决类似
的问题<Setter Property="P" Value="V" />
您必须了解 P(实际上是 P 的类型)才能理解如何将 V 类型转换为正确类型的值。 您用 [Ambient] 和 1. 标记“Property”属性。加载程序将首先处理“Property”,并且 2. 允许“Value”类型转换器在运行时读取“Type”值。
这也是如何{StaticResource foo}通过 XAML parent 查找 ResourceDictionary,其中可能包含“foo”。

例如:

// This markup extension returns the number of Ambient "Resource" properties
// Found in the XAML tree above it.
// The FrameworkElement.Resources property is already marked [Ambient]
public class MyMarkupExtension : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        var schemaProvider = serviceProvider.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider;
        var ambientProvider = serviceProvider.GetService(typeof(IAmbientProvider)) as IAmbientProvider;
        XamlMember resourcesProperty = new XamlMember(typeof(FrameworkElement).GetProperty("Resources"), schemaProvider.SchemaContext);
        List<AmbientPropertyValue> resources = (List<AmbientPropertyValue>) ambientProvider.GetAllAmbientValues(null, resourcesProperty);
        Debug.WriteLine("found {0} FramewrkElement.Resources Properties", resources.Count);
        return resources.Count.ToString();
    }
}

关于c# - 在 .NET 中使用 Ambient 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6646826/

相关文章:

PHP:反序列化已序列化并插入到 Mysql 数据库中的数据并回显到 PHP 中的表

c# - 如何在第二台显示器(屏幕)上隐藏部分窗口

c# - 默认 xml 命名空间在 XAML 中如何工作?

java - Jersey JSON/JAXB 忽略或看不到 POJO 中的 "some"公共(public)属性

c# - 没有 PRISM 的绑定(bind)命令

c# - const 多维数组初始化

c# - 使用DirectSound设置不同的音频输出

c# - 为什么在我的 asp.net 代码中多次创建我的静态对象?

xaml - 如何在 Kaxaml 中引用不同的 DLL

php - JSON.stringify() 对于序列化 JSON 对象是否可靠?