【问题标题】:How can I use a system color for gradients in Windows Phone 8.1?如何在 Windows Phone 8.1 中为渐变使用系统颜色?
【发布时间】:2015-12-13 13:05:50
【问题描述】:

我想要一个主题的系统颜色作为 Windows Phone 中LinearGradientBrush 的一部分。所以不是

<LinearGradientBrush x:Key="StandardGradientBackground"  EndPoint="0.5,1" StartPoint="0.5,0.5">
    <GradientStop Color="#FF660000" Offset="0"/>
    <GradientStop Color="#FFff0033" Offset="1"/>
</LinearGradientBrush>

我想使用类似的东西

<LinearGradientBrush x:Key="StandardGradientBackground"  EndPoint="0.5,1" StartPoint="0.5,0.5">
    <GradientStop Color="SystemColors.ActiveBorderColor" Offset="0"/>
    <GradientStop Color="#FFff0033" Offset="1"/>
</LinearGradientBrush>

我尝试了不同的语法,还阅读了this post,但正如 Visual Studio 所说,"static is not supported in a Windows App Project"

我也尝试以编程方式实现同​​样的目标

LinearGradientBrush linearGradientBrush =
    new LinearGradientBrush
    {
        StartPoint = new Point( 0.5, 0.5 ),
        EndPoint = new Point( 0.5, 1 )
    };
Color currentAccentColorHex = (Color)Current.Resources[ "PhoneAccentColor" ];
linearGradientBrush.GradientStops.Add( new GradientStop
{
    Color = currentAccentColorHex,
    Offset = 0
} );
linearGradientBrush.GradientStops.Add( new GradientStop
{
    Color = Colors.Black,
    Offset = 1
} );

一旦我到达尝试访问 (Application.)Current.Resources 的行,我就会得到一个 System.ExceptionCatastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

有什么想法吗?

更新

我正在尝试在App.xaml(对于编程方法分别为App.xaml.cs)文件中进行设置,以防万一这意味着需要考虑任何特殊步骤。

【问题讨论】:

  • 你试过this way吗? :((SolidColorBrush)App.Current.Resources["PhoneAccentBrush"]).Color
  • @har 感谢您的建议,不幸的是我最终得到了相同的结果。在访问 Current.Resources 正常之前,演员不会发生,所以原因可能在其他地方。

标签: c# xaml windows-phone-8.1


【解决方案1】:
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0.5">
    <GradientStop Color="{StaticResource SystemColorControlAccentColor}" Offset="0"/>
    <GradientStop Color="#FFff0033" Offset="1"/>
</LinearGradientBrush>

【讨论】:

  • Visual Studio 2015 下划线颜色属性和工具提示显示The resource "SystemColorControlAccentColor" could not be resolved - 尝试ThemeResource 而不是StaticResource 也不起作用。我正在尝试在App.xaml 文件中设置它。
  • 它适用于 WP8.1(也适用于 W8.1),但我没有在 VS2015 中对其进行测试。它虽然适用于VS2013。也许在 VS2015 中还有另一种“现代”语法。
  • 顺便说一句,您是否尝试仅编译此代码? VS2013 没有强调它,但 IntelliSense 也没有提出它。它可能会编译,所以试一试。
  • 你说得对,我认为它不起作用,因为 VS2015 用上述错误消息强调了它。奇怪的行为,如果它在实际工作时不会指示错误会更好。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多