[UWP] [C#] x:类对ResourceDictionaries不起作用 [英] [UWP][C#] x:Class Does Not Work for ResourceDictionaries

查看:94
本文介绍了[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:


  1. 使用VS 2017,使用空白应用
    项目创建新的UWP应用模板类型
  2. 为应用程序命名 IssueWithRD
  3. 使用默认目标版本
  4. 添加新资源字典并将其命名为 TestRD.xaml
  5. x:Class =" IssueWithRD.TestRD" 代码属性添加到
    ResourceDictionary
    TestRD.xaml
    的定义。
  6. 添加以下代码以更改 的默认颜色。刷(这样做是为了证明资源由UWP加载)

    < SolidColorBrush x:Key =" ApplicationPageBackgroundThemeBrush"颜色="红色" />
  7. 添加一个新的C#文件并将其命名为 TestRD.xaml.cs (这将作为
    TestRD.xaml 文件)
  8. 将TestRD类标记为部分
  9. 创建无参数构造函数该类(此时不要向正文添加任何内容)。
  10. 构建项目。
  11. 现在将 this.InitializeComponent(); 语句添加到构造函数
    * AND * 在此行上放置一个断点。
  12. 打开 App.xaml 文件,并将以下代码添加到合并
    TestRD.xaml 资源字典。

     < Application.Resources>

      < ResourceDictionary> ; $
       < ResourceDictionary.MergedDictionaries>

        < ResourceDictionary Source =" TestRD.xaml" />

       < /ResourceDictionary.MergedDictionaries>

      < / ResourceDictionary>

      ;< /Application.Resources>

  13. 运行应用程序。
  1. Using VS 2017, create a new UWP application using the Blank App project template type
  2. Name the application IssueWithRD
  3. Use the default target versions
  4. Add a new resource dictionary and name it TestRD.xaml.
  5. Add the x:Class="IssueWithRD.TestRD" code behind attribute to the ResourceDictionary definition of TestRD.xaml.
  6. 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" />
  7. Add a new C# file and name it TestRD.xaml.cs (this will serve as the code behind for the TestRD.xaml file)
  8. Mark the TestRD class as partial.
  9. Create a parameterless constructor for the class (do not add anything to the body at this time).
  10. Build the project.
  11. Now add this.InitializeComponent(); statement to the constructor *AND* place a break point on this line.
  12. 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>
  13. 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.com]

- Rashad Rivera [omegusprime.com]

推荐答案

资源字典在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.


这篇关于[UWP] [C#] x:类对ResourceDictionaries不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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