从 c# (Windows Phone) 配置主题资源的头痛 [英] Headaches with configuring themed resources from c# (Windows Phone)

查看:25
本文介绍了从 c# (Windows Phone) 配置主题资源的头痛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Windows Phone 应用,我想根据用户是深色主题还是浅色主题以及他们选择的强调色来拥有不同的主题资源.

I have Windows Phone App and I want to have a different themed resources based on whether the user has a dark or light theme and which accent color they have chosen.

在我的应用程序初始化代码中,我检测用户主题和强调色,然后加载适当的资源字典、深色、浅色等.我将资源字典添加到 App.Current.Resources.MergedDictionaries 集合.

In my app initialize code I detect the users theme and accent color then load the appropriate Resource Dictionary, Dark, Light etc. I add the Resource Dictionary to the App.Current.Resources.MergedDictionaries collection.

当我想从我的 App.Xaml 中引用(动态加载的)中的值时,问题就出现了.在下面的示例中,键是DefaultBackgroundImageOpacity".出于某种原因,当 App.Xaml 解析发生时,我加载到 MergedDictionaries 集合中的 ResourceDictionary 中的值永远不会被找到.在调用 InitializeComponent() 之前和之后,我已经尝试加载资源.两者似乎都无法解决问题.

The problem comes when I want to reference a value in the (dynamically loaded) from my App.Xaml. In the below example the key is "DefaultBackgroundImageOpacity". For some reason the values in the ResourceDictionary that I load into the MergedDictionaries collection never get found when the App.Xaml parsing happens. I've played with loading the resources before I call InitializeComponent() and after. Neither seem to resolve the issue.

有什么想法吗?

// Simplified version of adding some xaml...
// Note I'm loading the key "DefaultBackgroundImageOpacity"
this.Resources.MergedDictionaries.Clear();

var myTestXaml = "<ResourceDictionary xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' xmlns:System='clr-namespace:System;assembly=mscorlib'> <System:Double x:Key='DefaultBackgroundImageOpacity'>0.2</System:Double></ResourceDictionary>";

this.Resources.MergedDictionaries.Add((ResourceDictionary)XamlReader.Load(myTestXaml));

// This always fails saying that the key "DefaultBackgroundImageOpacity"
// can not be foudnd - even though it was loaded just above
InitializeComponent();

// Phone-specific initialization
InitializePhoneApplication();

推荐答案

我怀疑您的 App.Xaml 包含以下内容:-

I suspect your App.Xaml contains something like this:-

<Application.Resources>
   <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="/somedictionary.xaml" />
          ...
       </ResourceDictionary.MergedDictionaries>
       ...
  </ResourceDictionary>
</Application.Resources>

如果是这样,则 InitializeComponent 将替换您在代码中操作的默认 ResourceDictionary,因此您对 MergeDictionaries 的更改将不会存在,因为 ResourceDictionary 的那个实例不再被引用.

If that is so then InitializeComponent will replace the default ResourceDictionary which you have manipulated in your code hence your changes to MergeDictionaries will not be present since that instance of a ResourceDictionary is no longer referenced.

如果您没有在 App.Xaml 中设置任何 MergedDictionaries,请确保将资源直接添加到 Applicaiton.Resources 并且没有创建 MergedDictionaries 的新实例代码>ResourceDictionary 在 xaml 中.

If you are not setting up any MergedDictionaries in the App.Xaml then make sure you add resources directly to Applicaiton.Resources and are not creating an new instance of ResourceDictionary in the xaml.

这篇关于从 c# (Windows Phone) 配置主题资源的头痛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆