本文介绍了[UWP] [C#] x:类对ResourceDictionaries
不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello All,
Hello All,
我希望在某些条件下插入一些样式。在我目前的情况下,我想通过添加"Debug Build"来修改全局Frame样式。直接在内容上方的横幅。我在WPF中熟悉的方法是在运行时通过主ResourceDictionary的代码将合并到其他ResourceDictionaries中。
I want to have some styles inserted under certain condition. In my current case, I want to change modify the global Frame style by adding a "Debug Build" banner directly above the content. The method I'm familiar with in WPF is to merge
in other ResourceDictionaries at runtime via a main ResourceDictionary's code behind.
不幸的是,UWP似乎不支持ResourceDictionaries的代码支持。有人可以告诉我我做错了什么或建议替代方案吗?我不认为主题是一种选择,因为它会导致我只为一个小横幅创建主题的完整副本。
Unfortunately, UWP does not seem to support code behinds for ResourceDictionaries. Can someone tell me what I'm doing wrong or suggest an alternative? I don't see theming as an option because it would cause me to create entire copies of themes
just for one tiny banner.
下面是一个证明我的发现的演练:
Below is a walkthrough that proves my findings:
使用VS 2017,使用空白应用 项目创建新的UWP应用模板类型 为应用程序命名 IssueWithRD 使用默认目标版本 添加新资源字典并将其命名为 TestRD.xaml 。 将 x:Class =" IssueWithRD.TestRD" 代码属性添加到 ResourceDictionary TestRD.xaml 的定义。 添加以下代码以更改的默认颜色。刷(这样做是为了证明资源由UWP加载) < SolidColorBrush x:Key =" ApplicationPageBackgroundThemeBrush"颜色="红色" /> 添加一个新的C#文件并将其命名为 TestRD.xaml.cs (这将作为 TestRD.xaml 文件) 将TestRD类标记为部分。 创建无参数构造函数该类(此时不要向正文添加任何内容)。 构建项目。 现在将 this.InitializeComponent(); 语句添加到构造函数 * AND * 在此行上放置一个断点。 打开 App.xaml 文件,并将以下代码添加到合并 TestRD.xaml 资源字典。 < Application.Resources> < ResourceDictionary> ; $ < ResourceDictionary.MergedDictionaries> < ResourceDictionary Source =" TestRD.xaml" /> < /ResourceDictionary.MergedDictionaries> < / ResourceDictionary>   ;< /Application.Resources> 运行应用程序。
Using VS 2017, create a new UWP application using the Blank App
project template typeName the application IssueWithRDUse the default target versionsAdd a new resource dictionary and name it TestRD.xaml.Add the x:Class="IssueWithRD.TestRD" code behind attribute to the
ResourceDictionary definition of
TestRD.xaml.Add the following code to change the default color of the brush (this is done to prove that the resource is loaded by UWP)
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="Red" />Add a new C# file and name it TestRD.xaml.cs (this will serve as the code behind for the
TestRD.xaml file)Mark the TestRD class as partial.Create a parameterless constructor for the class (do not add anything to the body at this time).Build the project.Now add this.InitializeComponent(); statement to the constructor
*AND* place a break point on this line.Open the App.xaml file and add the following code to merge the
TestRD.xaml resource dictionary.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TestRD.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Run the application.
您会注意到背景更改为红色证明加载了资源字典,但断点不会被击中。
You will notice that the background changes to Red proving that the resource dictionary loaded, but the break point will not be hit.
- Rashad Rivera [omegusprime]
- Rashad Rivera [omegusprime]
推荐答案
资源字典在UWP中没有代码隐藏。您可以通过编程方式加载项目中的资源字典,如下所示:
Resource Dictionaries don't have code-behind in UWP. You can programmatically load a resource dictionary that's in your project like this:
var resourceDictionary = new ResourceDictionary { Source = new Uri("ms-appx:///TestRD.xaml") };
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
App.xams.cs中 OnLaunched 方法的开头很好如果您想修改全局样式,请拨打此电话。
The start of the OnLaunched method in App.xams.cs is a good place for this call, if you want to modify global styles.
发布评论