在 Windows Phone 8.1 中以编程方式将控件样式设置为 ThemeResource 值

Set Control Styles to ThemeResource Values Programmatically In Windows Phone 8.1

我正在 Windows Phone 8.1 应用程序中动态添加一些控件。在XAML中,你可以为当前主题的样式设置各种样式,就像我在下面的例子中设置这个TextBlock控件的foreground属性一样。

<TextBlock Text="Hello World" Foreground="{ThemeResource PhoneAccentBrush}" />

我希望能够在后面的代码中做同样的事情,但还不能确定如何做。我按如下方式以编程方式创建 TextBlock。

TextBlock textBlock = new TextBlock() {
    Text = "Hello World",
    Foreground = // Need to get phone accent brush from theme
};

我看到过存储不同主题值的示例如下,但是当我检查主题资源时,这个词典似乎不包含任何键。

SolidColorBrush phoneAccent = new SolidColorBrush((Color)Application.Current.Resources["PhoneAccentColor"]);

如有任何帮助,我们将不胜感激。谢谢!

从 PhoneAccentBrush 而非 PhoneAccentColor 加载它:

Brush accentBrush = Resources["PhoneAccentBrush"] as Brush;
TextBlock textBlock = new TextBlock()
{
    Text = "Hello World",
    Foreground = accentBrush 
};

在 Visual Basic .net 中:

.Foreground = CType(Resources.Item("PhoneAccentBrush"), Brush)