为什么应用程序级资源不能工作

本文关键字:不能 工作 资源 应用程序 为什么 | 更新日期: 2023-09-27 17:51:11

我想在我的页面中实现应用程序级别的资源,但似乎它不工作。

App.xaml:

<Application.Resources>
<Style TargetType="phone:PhoneApplicationPage">
   <Setter Property="Background" Value="White" />
   <Setter Property="Foreground" Value="Black" />
   <Setter Property="shell:SystemTray.ForegroundColor" Value="Black"></Setter>
   <Setter Property="shell:SystemTray.BackgroundColor" Value="Transparent"></Setter>
   <Setter Property="shell:SystemTray.Opacity" Value="0.5"></Setter>
</Style>
</Application.Resources>
页面

<phone:PhoneApplicationPage
...
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True" >
</phone:PhoneApplicationPage>

我希望系统托盘应该看起来像它在App.xaml中的样式,但它没有。

谢谢!

为什么应用程序级资源不能工作

应用范围内的隐式样式从Mango开始在Windows Phone上工作。您需要记住的是,隐式样式只应用于TargetType,而不应用于它的后代。你的Pages是新的类,从PhoneApplicationPage继承。可以这样做:

<Application.Resources>
  <Style TargetType="local:MainPage">
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="shell:SystemTray.ForegroundColor" Value="Black"/>
  </Style>
</Application.Resources>

但是很明显,您必须为项目中的每个页面定义它,这使得它毫无用处。对于页面,最好使用显式样式(带有x:Key):

<Application.Resources>
  <Style x:Key="PageStyle" TargetType="phone:PhoneApplicationPage">
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="shell:SystemTray.ForegroundColor" Value="Black"/>
  </Style>
</Application.Resources>

对于项目中的每个页面只需:

<phone:PhoneApplicationPage 
  Style="{StaticResource PageStyle}"