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

时间:2015-04-09 22:43:12

标签: c# xaml windows-phone-8.1

我在Windows Phone 8.1应用程序中动态添加了一些控件。在XAML中,您可以将各种样式设置为当前主题的样式,就像我在以下示例中设置此TextBlock控件的前景属性一样。

<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"]);

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

从PhoneAccentBrush加载而不是PhoneAccentColor:

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

答案 1 :(得分:0)

在Visual Basic .net中:

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