【问题标题】:How to change Color define in Xaml Resources in code (UWP)如何在代码 (UWP) 中更改 Xaml 资源中定义的颜色
【发布时间】:2017-07-24 14:34:44
【问题描述】:

[UWP]

我有许多来自 App.xaml 的颜色绑定网格

MainPage.xaml ...

        <Grid
            Height="45"
            Margin="0,0,0,10"
            Background="{ThemeResource MyColor}">

App.xaml

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Dark">
    <Application.Resources>
        <ResourceDictionary>
            <SolidColorBrush x:Key="MyColor">#FFFFFF</SolidColorBrush>

然后我想用这样的代码更改它的所有内容

    Application.Current.Resources["MyColor"] = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 242, 101, 34));

但它不起作用。我可以错过什么吗?当我导航到另一个页面并返回时,上面的代码会抛出 System.Exception

【问题讨论】:

  • 什么意思,它不起作用?是否抛出异常,Grids 的背景颜色是否无法改变?
  • 颜色不变。没有例外
  • 当我导航到另一个页面并返回时,上面的代码会抛出 System.Exception

标签: c# wpf xaml uwp uwp-xaml


【解决方案1】:

StaticResourceThemeResource 不支持动态更改,因为您尝试像 WPF 中的 DynamicResource 一样。顺便说一句,如果您重新加载视图(例如来回导航),您可以看到更改,但这不是一个好的解决方案。

另一方面,您可以使用ThemeResource 实现一些动态更改并更改例如。颜色取决于当前主题(深色、浅色、高对比度)

延伸阅读:https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/xaml-theme-resources

【讨论】:

  • 但我看到商店中的一些应用程序可以做到这一点。用户选择颜色后立即改变颜色(例如:来自 Windows 10 商店的 Awesome Tube)
  • 他们很可能是通过绑定来实现的,而不是像您那样使用代码。
【解决方案2】:

如果您知道它是 SolidColorBrush,则直接修改 Color 属性。

var brush = (SolidColorBrush)Application.Current.Resources["MyColor"];
brush.Color = Windows.UI.Color.FromArgb(255, 242, 101, 34);

您无法更改资源,但如果您有权访问,则可以修改其属性。

【讨论】:

    【解决方案3】:

    我是通过以下方式做到的:

    App.xaml

    <Application
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        RequestedTheme="Dark">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.ThemeDictionaries>
                <ResourceDictionary x:Key="Dark">
                    <Color x:Key="UserAccentColor">#FFFFA500</Color>
                    <SolidColorBrush x:Key="UserAccentBrush" Color="{StaticResource UserAccentColor}"/>
                </ResourceDictionary>
                <ResourceDictionary x:Key="Light">
                    <Color x:Key="UserAccentColor">#FFFFA500</Color>
                    <SolidColorBrush x:Key="UserAccentBrush" Color="{StaticResource UserAccentColor}"/>
                </ResourceDictionary>
    

    改变颜色:

    foreach (var dict in App.Current.Resources.ThemeDictionaries)
    {
        var theme = dict.Value as Windows.UI.Xaml.ResourceDictionary;
        ((SolidColorBrush)theme["UserAccentBrush"]).Color = color;
    }
    

    【讨论】:

      【解决方案4】:
      (App.Current.Resources["MyColor"] as SolidColorBrush).Color = Windows.UI.Color.FromArgb(255, 242, 101, 34);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 2020-04-13
        相关资源
        最近更新 更多