根据浅色/深色主题在运行时切换 XAML 资源?
我有一个使用 MahApps(“浅色”模式)的 WPF 应用程序,现在我为用户提供了一种切换模式(浅色/深色)的方法。许多应用程序的颜色显然会根据模式而变化,通过比较 \Styles\Themes\Light.colour_name.xaml 和Dark.colour_name.xaml 文件,但我想通过实现我自己的 xaml
资源以某种方式“扩展”这些资源,这些资源也会根据所选内容而变化模式。这可以做到吗?
一个例子是当前使用蓝色来绘制点的图表 - 这些在“亮”模式下看起来很好,但在“暗”模式下有点暗。我意识到我可以使用更合适的蓝色阴影,在两种模式下看起来都不错,但如果我可以定义一个颜色资源(例如)在浅色模式下为“蓝色”,但在深色模式下为“石灰”,那就更灵活了。这种功能也可能有其他用途,例如彩色通知消息。
I have a WPF application that uses MahApps ("light" mode) and I've now provided a way for the user to switch modes (light/dark). A lot of the app's colours obviously change depending on the mode, as can be seen by comparing the \Styles\Themes\Light.colour_name.xaml & Dark.colour_name.xaml files, but I would like to "extend" these in some way by implementing my own xaml <Color>
resources that also change depending on the selected mode. Can this be done?
One example is charts that currently use blue to plot the points - these look fine in "light" mode but are a bit dark in "dark" mode. I realise I could just use a more suitable shade of blue that looks okay in both modes, but it would be more flexible if I could define a Color resource that is (say) 'Blue' in light mode but 'Lime' in dark mode. There are likely to be other uses for such a feature too, e.g. coloured notification messages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以根据内置主题创建自定义主题。创建一个资源字典,合并基本主题的资源字典并覆盖现有画笔并添加新画笔。
浅色主题字典 (
Light.Blue.Custom.xaml
):深色主题字典 (
Dark.Blue.Custom.xaml
):在应用程序中注册主题字典。您必须将虚拟名称
YourApp
替换为类和资源字典 URI 中应用程序的真实名称。请注意,如果您想切换模式,则需要为两者浅色和深色提供主题。有关主题和自定义的更多信息,您可以参阅文档。
You could create custom themes based on the built-in themes. Create a resource dictionary that merges the resource dictionary of the base theme and override existing brushes and add new ones.
Light theme dictionary (
Light.Blue.Custom.xaml
):Dark theme dictionary (
Dark.Blue.Custom.xaml
):Register the theme dictionary in your application. You must replace the dummy name
YourApp
with the real name of your application in both the class and the resource dictionary URI.Note that you need to provide themes for both Light and Dark, if you want to switch modes. For more information about themes and customization, you can refer to the documentation.