locked
[UWP][C#] x:Class Does Not Work for ResourceDictionaries RRS feed

  • Question

  • Hello All,

    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.  

    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. 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]

    Friday, November 17, 2017 7:54 PM

All replies

  • 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);
    The start of the OnLaunched method in App.xams.cs is a good place for this call, if you want to modify global styles.

    Saturday, November 18, 2017 2:23 AM
  • @Rashad Rivera

    Have you solved your problem?

    Please feel free to contact us if you have any problems.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 5, 2017 2:42 AM