C# 向生成的文本块添加样式

C# 向生成的文本块添加样式,c#,windows-phone-7,windows-phone,C#,Windows Phone 7,Windows Phone,我想向生成的文本块添加自定义样式 TextBlock title = new TextBlock(); title.Style = (Style) Application.Current.Resources["styleTheke"]; title.Text = "test"; stackMenu.Children.Add(title); 此样式在中定义 <phone:PhoneApplicationPage.Resour

我想向生成的文本块添加自定义样式

        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

中定义的资源,如果该资源位于同一页面中,则可以通过以下方式对其进行引用:

(Style) Resources["styleTheke"];

Application.Current.Resources
字典是为
App.xaml

中定义的资源编写的(通常)。一旦我的程序达到那个点,我会得到一个异常,它声明“未指定的错误”。(此.Resources也会发生同样的情况。@theXs项目在显示时在动态生成的文本块中使用了这两种类型的资源。我在代码中发现了错误。在我取出属性“width”后,您无法将其设置为“AUTO”——它起作用了。我之前尝试过。一旦我的程序达到这一点,我会得到一个异常,其中指出“未指定的错误”。(此.Resources..@theXs项目在显示时在动态生成的TextBlocks中使用这两种类型的资源。我在代码中找到了错误。在我取出属性“width”后,您不能将属性“width”设置为“AUTO”-成功了。