为什么这个页面没有使用资源? [英] Why are resources not used in this page?

查看:21
本文介绍了为什么这个页面没有使用资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论我把资源放在哪里,它们的价值都没有被利用:

No matter where I put my resources, their value isn't used:

TextBlock 文本应该呈现为黑色,但始终呈现为白色.

The TextBlock text should be rendered black, but it's always rendered white.

为什么?

<Page
  x:Class="TemplateTest.MainPage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d" Background="Lavender">

  <Page.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="Foreground" Value="Black" />
    </Style>
  </Page.Resources>

  <StackPanel>
    <ListView>
      <ListView.HeaderTemplate>
        <DataTemplate>
          <TextBlock>
            <Run>Test</Run>
            <Run>!</Run>
          </TextBlock>
        </DataTemplate>
      </ListView.HeaderTemplate>

      <ListView.ItemTemplate>
        <DataTemplate x:DataType="x:String">
          <TextBlock Text="{x:Bind}"></TextBlock>
        </DataTemplate>
      </ListView.ItemTemplate>

      <x:String>Number 1</x:String>
      <x:String>Number 2</x:String>
      <x:String>Number 3</x:String>
    </ListView>
  </StackPanel>
</Page>

推荐答案

来自 Jim Walker @ https://github.com/MicrosoftDocs/windows-uwp/issues/2790#issuecomment-722065687:

From Jim Walker @ https://github.com/MicrosoftDocs/windows-uwp/issues/2790#issuecomment-722065687 :

TextBlock 与大多数控件的不同之处在于它没有可编辑的模板,因此不幸的是,它不支持轻量级样式.您必须使用 Style 来设置前景.而且,正如我们已经知道的,页面或应用程序级别的隐式样式不会应用于 DataTemplate 中的 TextBlocks,因此您必须使用单独的隐式样式Style 内的 DataTemplateStyle 带有更改那些 TextBlock 的键.据我所知,没有办法解决这个问题.

TextBlock is different from most controls in that it doesn't have an editable template, so, unfortunately, it doesn't support lightweight styling. You have to use a Style to set the foreground. And, as we already know, a Page- or App-level implicit style won't be applied to TextBlocks inside a DataTemplate, so you have to use a separate implicit Style inside the DataTemplate or a Style with a key to change those TextBlocks . As far as I know, there isn't any way around this.

这篇关于为什么这个页面没有使用资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆