什么是Silverlight 3.0中对于BasedOn="{StaticResource {x:Type TextBlock}}"的等效替代?

5

我试图扩展一个TextBlock的基础样式。在WPF中是很简单的事情,应该在Silverlight中也一样。但是我遇到了一个关于x:Type的错误。

如何在Silverlight中将BasedOn="{StaticResource {x:Type TextBlock}}"进行翻译。 有没有人做到了这一点?

谢谢。


你现在可以在Silverlight 5中实现这个功能。请查看我的下面的回答。 - Dr. Andrew Burnett-Thompson
4个回答

5
在Silverlight中没有与该特定用法相对应的内容。 Silverlight仅支持使用字符串键访问资源。 因此,使用{x:Type SomeType}作为键是不起作用的。
在Silverlight中,您需要完全复制控件样式。 您可以通过使用Blend进行此操作,后者具有执行此操作的工具,或者从Silverlight文档中复制并粘贴它。控件样式和模板 当然,一旦您拥有初始样式的副本,您就可以修改副本或创建其他样式,将此副本分配给BasedOn以创建一组变体。

嗨,安东尼,我就怕有人会这么说,那么只能开始工作了 :( - Calin

3
我认为稍微倒退一点会有所帮助,你可以先制定好基本样式。
<Style TargetType="Button" x:Key="MyButtonStyle">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>

那么你可以基于该样式来制作所有按钮。
<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}" />

那么,如果你需要在该样式基础上进行修改,只需以命名样式为基础即可。
<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>

或者

<Style TargetType="Button" BasedOn="{StaticResource MyButtonStyle}" x:Key="MyOtherButtonStyle">
    <Setter Property="PropertyName" Value="PropertyValue" />
</Style>

0

现在在Silverlight 5中,您实际上可以这样做。

首先,声明您的样式

<Style x:Key="TextBoxStyle" TargetType="TextBox" BasedOn="{local:Type TypeName=TextBox}">

</Style>

接下来,您需要创建一个MarkupExtension,它可以在WPF和Silverlight 5中工作,以替换x:Type。
/// A MarkupExtension which introduces x:Type like syntax to both WPF and Silverlight (Cross-platform). This is used internally
/// for the themes, but is also useful e.g. when creating custom Control Templates for SciChart
/// </summary>
/// <remarks>
/// Licensed under the CodeProject Open License
/// http://www.codeproject.com/Articles/305932/Static-and-Type-markup-extensions-for-Silverlight
/// </remarks>
/// 
public class TypeExtension : MarkupExtension
{
    /// <summary>
    /// Initializes a new instance of the <see cref="TypeExtension" /> class.
    /// </summary>
    public TypeExtension()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="TypeExtension" /> class.
    /// </summary>
    /// <param name="type">The type to wrap</param>
    public TypeExtension(Type type)
    {
        Type = type;
    }

    /// <summary>
    /// Gets or sets the type information for this extension.
    /// </summary>
    public System.Type Type { get; set; }

    /// <summary>
    /// Gets or sets the type name represented by this markup extension.
    /// </summary>
    public String TypeName { get; set; }

    public override Object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Type == null)
        {
            if (String.IsNullOrWhiteSpace(TypeName)) throw new InvalidOperationException("No TypeName or Type specified.");
            if (serviceProvider == null) return DependencyProperty.UnsetValue;

            IXamlTypeResolver resolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
            if (resolver == null) return DependencyProperty.UnsetValue;

            Type = resolver.Resolve(TypeName);
        }
        return Type;
    }
}

已在 WPF 和 Silverlight 中测试可用


0

嗨 Muad'Dib,我收到了“找不到名称/密钥TextBlock的资源”的错误提示,但实际上这并不正确,因为我明确使用的是silverlight工具包中的TwilightBlueTheme。 - Calin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接