本文介绍了如何阅读在C#Windows.UI.XAML.Style性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个类,将一个HTML文档转换为可在Windows 8应用RichTextBlock使用Paragrpahs列表。我希望能够给类XAML定义,该类将从样式读有用的特性,并将其应用样式列表。

I am writing a class that will convert a HTML document to a list of Paragrpahs that can be used with RichTextBlock in Windows 8 apps. I want to be able to give the class a list of Styles defined in XAML and the class will read useful properties from the style and apply them.

如果我有一个Windows .UI.XAML.Style 风格我怎么读它的属性?我试过

If I have a Windows.UI.XAML.Style style how do I read a property from it? I tried

var fontWeight = style.GetValue(TextElement.FontWeightProperty)

风格在XAML中定义的TargetProperty =TextBlock的,但这种失败和异常

for a style defined in XAML with TargetProperty="TextBlock" but this fails with and exception

推荐答案

您可以试试这个:

var fontWeightSetter =
    style.Setters.Cast<Setter>().FirstOrDefault(
        setter => setter.Property == TextElement.FontWeightProperty);

var fontWeight =
    fontWeightSetter != null ?
        (FontWeight)fontWeightSetter.Value :
        FontWeights.Normal;

或检查其是否正常工作:

Or check if that works:

public static class StyleExtensions
{
    // Untested
    public static object GetPropertyValue(this Style style, DependencyProperty property)
    {
        var setter =
            style.Setters.Cast<Setter>().FirstOrDefault(
                s => s.Property == property);
        var value = setter != null ? setter.Value : null;

        if (setter == null &&
            style.BasedOn != null)
        {
            value = style.BasedOn.GetPropertyValue(property);
        }

        return value;
    }
}

这篇关于如何阅读在C#Windows.UI.XAML.Style性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 13:30