2

如果您自己编写,您MarkupExtension可能会偶然发现IServiceProvider serviceProvider- 参数在设计时与运行时不同。例如,您无法获得IRootObjectProvider. 那么是否有任何关于该Interfaces参数对象实现的文档以及是否有任何解决方法来获取根对象?

4

2 回答 2

2

写了一个小助手来查看可用的服务:

public class ServiceExtension : MarkupExtension
{
    public ServiceExtension()
    {
    }

    public ServiceExtension(Type serviceType)
    {
        ServiceType = serviceType;
    }

    [ConstructorArgument("serviceType")]
    public Type ServiceType { get; set; }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public object Service { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        if (ServiceType == null)
        {
            Service= "ServiceType == null";
            return this;
        }
        var service = serviceProvider.GetService(ServiceType);
        Service= service ?? "null";
        return this;
    }
}

<Style TargetType="{x:Type HeaderedContentControl}">
    <Setter Property="Header" Value="{Binding ServiceType}" />
    <Setter Property="Content" Value="{Binding Service}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedContentControl}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Col1" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="0"
                                      Margin="0,0,5,0"
                                      ContentSource="Header" />
                    <ContentPresenter Grid.Column="1" ContentSource="Content" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ItemsControl Grid.IsSharedSizeScope="True">
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlNameProvider}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlNameResolver}}" />

    <HeaderedContentControl DataContext="{local:Service {x:Type x:IProvideValueTarget}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type x:IXamlTypeResolver}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type x:IValueSerializerContext}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type componentModel:ITypeDescriptorContext}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IDestinationTypeProvider}}" />

    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlNamespaceResolver}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IXamlSchemaContextProvider}}" />

    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IRootObjectProvider}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type x:IUriContext}}" />
    <HeaderedContentControl DataContext="{local:Service {x:Type xaml:IAmbientProvider}}" />
</ItemsControl>
于 2015-10-03T00:57:18.387 回答