WP7 项目中的主题感知 XAML 资源

发布于 2024-10-11 13:21:42 字数 1432 浏览 1 评论 0 原文

我正在制作一个 Windows Phone 7 应用程序,但我对深色/浅色主题有点困惑。

对于全景图,您经常会设置背景图像。问题是很难制作出既适合深色主题又适合浅色主题的图片。我们应该如何进行?

有没有办法强制全景图采用深色/浅色主题?这样可以避免制作特定主题的全景背景图片。那我该怎么办呢?我在 C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Design 中找到了 xaml 文件。如果这是正确的方法,我如何将它们导入到我的全景图中?

或者,如果无法(或者错误)强制使用深色/浅色主题:如何编写条件 XAML 来设置正确的资源?现在我有以下 XAML (default.xaml),它适用于深色主题:

<ImageBrush x:Key="PageBackground" ImageSource="Resources/PageBackground.png" Stretch="None" />
<ImageBrush x:Key="PanoramaBackground" ImageSource="Resources/PanoramaBackground.png" Stretch="None" />

但是当我使用浅色主题时,黑色控件和黑色文本很难用深色背景图片阅读。所以我制作了不同的图片,我可以这样使用:

<ImageBrush x:Key="PageBackground" ImageSource="Resources/PageBackgroundLight.png" Stretch="None" />
<ImageBrush x:Key="PanoramaBackground" ImageSource="Resources/PanoramaBackgroundLight.png" Stretch="None" />

现在我的问题是使 XAML 有条件地根据当前主题声明正确的内容。

我在网上没有找到相关的方法。我不想为此使用代码或代码隐藏,因为我相信 XAML 能够做到这一点(我只是不知道如何做到)。

编辑:将 xaml 文件加载为 ResourceDictionary 的代码片段

string xaml = null;
StreamResourceInfo xamlInfo = Application.GetResourceStream(new Uri("light.xaml", UriKind.Relative));
using (StreamReader sr = new StreamReader(xamlInfo.Stream))
    xaml = sr.ReadToEnd();
dic = (ResourceDictionary)XamlReader.Load(xaml);
this.Resources.MergedDictionaries.Add(dic);

I'm making a Windows Phone 7 application and I'm a bit confused with dark/light themes.

With a panorama, you very often set a background image. The issue is it's very hard to make a picture which is right for both dark and light themes. How are we supposed to proceed?

Is there a way to force a dark/light theme for a panorama? This will avoid making theme-specific panorama background pictures. Then how do I do? I found xaml files in C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Design. If this is a right way to proceed, how can I import them for my panorama?

Or if there's no way (or if it's wrong) to force a dark/light theme: how to write conditional XAML to set correct resources? Now I have the following XAML (default.xaml) which is fine with the dark theme:

<ImageBrush x:Key="PageBackground" ImageSource="Resources/PageBackground.png" Stretch="None" />
<ImageBrush x:Key="PanoramaBackground" ImageSource="Resources/PanoramaBackground.png" Stretch="None" />

But when I use a light theme, black controls and black texts are hard to read with my dark background pictures. So I made different pictures that I can use this way:

<ImageBrush x:Key="PageBackground" ImageSource="Resources/PageBackgroundLight.png" Stretch="None" />
<ImageBrush x:Key="PanoramaBackground" ImageSource="Resources/PanoramaBackgroundLight.png" Stretch="None" />

Now my issue is to make XAML conditional to declare the right thing depending on the current theme.

I found no relevant way on the Internet. I would prefer not to use code or code-behind for that because I believe XAML is able to do this (I just don't know how).

EDIT: Code snippet to load a xaml file as ResourceDictionary

string xaml = null;
StreamResourceInfo xamlInfo = Application.GetResourceStream(new Uri("light.xaml", UriKind.Relative));
using (StreamReader sr = new StreamReader(xamlInfo.Stream))
    xaml = sr.ReadToEnd();
dic = (ResourceDictionary)XamlReader.Load(xaml);
this.Resources.MergedDictionaries.Add(dic);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-10-18 13:21:42

要强制使用深色或白色主题,您确实可以使用您指出的文件夹中定义的样式。将您需要的规则复制并粘贴到 App.xaml 中(只需 PhoneForegroundColor、PhoneBackgroundColor 和相关画笔就是一个好的开始)。

不过,保持“主题意识”并为浅色和深色主题加载不同的图像可能会更好。这是一篇解释如何执行此操作的文章:http ://blog.jayway.com/2010/12/16/theme-aware-panorama-background-in-windows-phone-7/

To force a dark or white theme you can indeed use the styles defined in the folder you pointed out. Copy and Paste the rules you need to your App.xaml (just PhoneForegroundColor, PhoneBackgroundColor and the related Brushes would be a good start).

It's probably better though to stay "theme-aware" and load a different image for light and dark themes. Here is an article explaining how to do this: http://blog.jayway.com/2010/12/16/theme-aware-panorama-background-in-windows-phone-7/

烟织青萝梦 2024-10-18 13:21:42

我发现了另一种可能性:您可以根据 Coding4Fun Toolkit 转换器://windowsphonegeek.com/tips/Dynamically-Set-Theme-based-Images-in-Windows-Phone-using-ThemedImageConverter" rel="nofollow">这些说明。但是,我无法正确使用它们。

另一种可能性是使用 不透明蒙版。但这仅适用于黑白图像:/

Yousef 的解决方案看起来很有趣。 但是加载时间太长。应用程序启动后约 1 秒图像将发生变化。我已经在诺基亚 820 上对此进行了测试。 我已将在 Loaded Event 中设置 DataContext 的调用移至稍后调用。现在调用发生在构造函数中,因此当应用程序显示图像时图像将已经设置。然而,它仍然增加了更多的加载时间:( 关于如何改进这个问题有什么建议吗?

There is another possibility I've found: You can use the Coding4Fun Toolkit Converter according to these instructions. However, I'm unable to use correctly use them.

Another possibiliy is to use an OpacityMask. But this only works for black/white images :/

Yousef's solution looks interesting. but it takes too much time to load. The image will be changed round about 1s after the app started. I've tested this on a Nokia 820. I've moved the call for setting the DataContext in a Loaded Event, which was called much later. Now the call takes place in the constructor, so the image will be already set when the application displays it. However, it still adds more loading time :( Any suggestions on how to improve this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文