有人可以阐明 .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/