我想在生成的文本块中添加自定义样式。

        TextBlock title = new TextBlock();
        title.Style = (Style) Application.Current.Resources["styleTheke"];
        title.Text = "test";
        stackMenu.Children.Add(title);

此样式定义在

 <phone:PhoneApplicationPage.Resources>
  <Style x:Key="styleTheke" TargetType="TextBlock">
   <Setter Property="Width" Value="Auto"/>
   <Setter Property="Height" Value="40"/>
   <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/>
   <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
  </Style>
 </phone:PhoneApplicationPage.Resources>

但是..文本块总是显示为“未风格”。

有帮助吗?

解决方案

如果资源在同一页面中,则可以通过:

(Style) Resources["styleTheke"];

Application.Current.Resources 字典是为(通常)定义的资源 App.xaml.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top