C# 在C中读取Windows.UI.XAML.Style属性#

C# 在C中读取Windows.UI.XAML.Style属性#,c#,xaml,windows-phone-8.1,C#,Xaml,Windows Phone 8.1,我正在尝试将HTML字符串转换为RichTextBlock内容。为了做到这一点,我正在使用内联线,我想使用XAML中定义的样式设置这些内联线的样式属性 我已经找到了一个解决方案并找到了答案。但是,这段代码返回的结果是bindinexpressionbase,我无法将其设置为直接运行属性。我尝试了不同的方法,但没有成功 这是我的XAML <Style x:Key="RTCodeStyle" TargetType="HyperlinkButton" BasedOn="{StaticResour

我正在尝试将HTML字符串转换为RichTextBlock内容。为了做到这一点,我正在使用内联线,我想使用XAML中定义的样式设置这些内联线的样式属性

我已经找到了一个解决方案并找到了答案。但是,这段代码返回的结果是
bindinexpressionbase
,我无法将其设置为直接运行属性。我尝试了不同的方法,但没有成功

这是我的XAML

<Style x:Key="RTCodeStyle" TargetType="HyperlinkButton" BasedOn="{StaticResource BaseRichTextBlockStyle}">
    <Setter Property="FontFamily" Value="Courier New" />
    <Setter Property="FontSize" Value="{ThemeResource TextStyleLargeFontSize}" />
    <Setter Property="Foreground" Value="Gray" />
</Style>
此代码导致
无法将类型“object”隐式转换为“Windows.UI.Xaml.Media.FontFamily”。存在显式转换(是否缺少转换?
错误。试图将返回值强制转换为
RichTextBlock.FontFamilyProperty
会导致
“Windows.UI.Xaml.Controls.RichTextBlock.FontFamilyProperty”是一个“属性”,但其用法类似于“类型”
错误

GetPropertyValue(from)的主体是

公共静态类样式扩展
{
公共静态对象GetPropertyValue(此样式,DependencyProperty属性)
{
var setter=style.Setters.Cast().FirstOrDefault(s=>s.Property==Property);
var value=setter!=null?setter.value:null;
if(setter==null&&style.BasedOn!=null)
value=style.BasedOn.GetPropertyValue(属性);
返回值;
}
}

感谢您提前提供的帮助。

您不需要将它转换为您感兴趣的元素吗

r.FontFamily = (FontFamily)style.GetPropertyValue(RichTextBlock.FontFamilyProperty);
r.FontSize = (FontSize)style.GetPropertyValue(RichTextBlock.FontSizeProperty);
r.Foreground = (Brush)style.GetPropertyValue(RichTextBlock.ForegroundProperty);

不幸的是,这也导致了一个异常<代码>无法将'style.GetPropertyValue(RichTextBlock.FontFamilyProperty)'(其实际类型为'Windows.UI.Xaml.Data.BindingExpressionBase')强制转换为'Windows.UI.Xaml.Media.FontFamily'@BirkanCilingir:将其强制转换为
BindingExpressionBase
,然后您就可以访问
Target
TargetProperty
属性,然后,它可以帮助您使用反射来实际读取值?
BindingExpressionBase
类属于
Windows.UI.Xaml.Data
命名空间(不是
System.Windows.Data
),并且它不包含
Target
TargetProperty
。而且,即使那个类似乎也没有
值,这让我觉得我的方法从一开始就不正确。
public static class StyleExtensions
{
    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;
    }
}
r.FontFamily = (FontFamily)style.GetPropertyValue(RichTextBlock.FontFamilyProperty);
r.FontSize = (FontSize)style.GetPropertyValue(RichTextBlock.FontSizeProperty);
r.Foreground = (Brush)style.GetPropertyValue(RichTextBlock.ForegroundProperty);