问题描述
限时送ChatGPT账号..[UWP]
我有很多来自 App.xaml 的带有颜色绑定的网格
I have many grids with color binding from App.xaml
MainPage.xaml ...
MainPage.xaml ...
<Grid
Height="45"
Margin="0,0,0,10"
Background="{ThemeResource MyColor}">
应用程序.xaml
<Application
xmlns="http://schemas.microsoft/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft/winfx/2006/xaml"
RequestedTheme="Dark">
<Application.Resources>
<ResourceDictionary>
<SolidColorBrush x:Key="MyColor">#FFFFFF</SolidColorBrush>
然后我想改变它的所有,在这样的代码中
Then I want to change all of its, in code like this
Application.Current.Resources["MyColor"] = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 242, 101, 34));
但它不起作用.我可以错过什么吗?当我导航到另一个页面并返回时,上面的代码抛出 System.Exception
But it not work. May I miss something? When I navigate to another page and navigate back, above code throw a System.Exception
推荐答案
StaticResource
和 ThemeResource
不支持像 DynamicResource
那样动态变化WPF 中的代码>.顺便说一句,如果您像前后导航一样重新加载视图,您可以看到更改,但这不是一个好的解决方案.
StaticResource
and ThemeResource
doesn't support dynamic change as you try like DynamicResource
in WPF. BTW if you reload the view like navigating back and forward you can see the changes but this is not a good solution.
另一方面,您可以使用 ThemeResource
实现一些动态更改并更改例如.颜色取决于当前主题(深色、浅色、高对比度)
Other hand, you can achieve some dynamic change with ThemeResource
and change eg. colors depend on the current theme (Dark, Light, High Contrast)
进一步阅读:https://docs.microsoft/en-us/windows/uwp/controls-and-patterns/xaml-theme-resources
这篇关于如何在代码 (UWP) 中更改 Xaml 资源中的颜色定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论