none
How to write ResourceDictionary in code behind RRS feed

  • Question

  • I have a resource dictionary in my App.xaml file but i need to modify this according to a theme color so how can i write this in code behind ?

    <Application.Resources>
            <ResourceDictionary>
                <Color x:Key="SystemAccentColor">#1892E7</Color>
            </ResourceDictionary>
    </Application.Resources>

     
    Thursday, November 7, 2019 5:25 AM

Answers

  • Hi,

    Is there still an error when you call it in the OnLaunched function?

    When I tested it locally, I found that I can modify the resources in the resource dictionary without error.

    In addition, if you are trying to modify the color for the theme change, it is recommended that you use ThemeResource and create two resource dictionaries, Light and Dark.

    Here is the document about this.

    Best regards.



    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    • Marked as answer by laxmi swami Monday, November 11, 2019 5:03 AM
    Monday, November 11, 2019 2:19 AM

All replies

  • Hi,

    Yes, you can modify the contents of the current Resources, please use this code:

    Application.Current.Resources["SystemAccentColor"] = Colors.Red;

    But there is a problem here. The resource is static and will not react to the UI immediately after the change, so you will see that the color of the control has not changed since the resource was modified.

    So after modifying the resource color, you need to adjust the RequestedTheme of the current page to trigger the UI change.

    Application.Current.Resources["SystemAccentColor"] =Colors.Red;
    if (this.RequestedTheme == ElementTheme.Light)
    {
        this.RequestedTheme = ElementTheme.Dark;
        this.RequestedTheme = ElementTheme.Light;
    }
    else if (this.RequestedTheme == ElementTheme.Dark)
    {
        this.RequestedTheme = ElementTheme.Light;
        this.RequestedTheme = ElementTheme.Dark;
    }
    else
    {
        this.RequestedTheme = ElementTheme.Light;
        this.RequestedTheme = ElementTheme.Dark;
        this.RequestedTheme = ElementTheme.Default;
    }
    

    Also, remember to use the ThemeResource in the XAML for a reference to the resource in order to trigger the change.

    ---

    However, please note that theme refresh is only suitable for FrameworkElement (such as Page), while App.Current.RequestedTheme is not supported for modification at runtime.

    Best regards.


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Thursday, November 7, 2019 6:56 AM
  • I have tried this but its throwing below error. 

    'Application.Current.Resources' threw an exception of type 'System.Exception'
        Data: {System.Collections.ListDictionaryInternal}
        HResult: -2147418113
        HelpLink: null
        InnerException: null
        Message: "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
        Source: "Windows"
        StackTrace: "   at Windows.UI.Xaml.Application.get_Resources()"
        TargetSite: {Windows.UI.Xaml.ResourceDictionary get_Resources()}

    Thursday, November 7, 2019 10:14 AM
  • Hi,

    The call to Application.Current.Resources["SystemAccentColor"] needs to meet some conditions.

    First, there is a resource named "SystemAccentColor" in your resource list.

    Second, it must wait for the application to be loaded after the call is completed. It cannot be directly call the resource in the App constructor.

    Can you post the context of the code? This is very helpful for us to analyze the problem.

    Best regards.


    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    Friday, November 8, 2019 3:05 AM
  • Thanks , I had to call that from onLaunched method.
    Sunday, November 10, 2019 5:51 PM
  • Hi,

    Is there still an error when you call it in the OnLaunched function?

    When I tested it locally, I found that I can modify the resources in the resource dictionary without error.

    In addition, if you are trying to modify the color for the theme change, it is recommended that you use ThemeResource and create two resource dictionaries, Light and Dark.

    Here is the document about this.

    Best regards.



    "Developing Universal Windows apps" forum will be migrating to a new home on Microsoft Q&A (Preview)!
    We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A (Preview)!
    For more information, please refer to the sticky post.

    • Marked as answer by laxmi swami Monday, November 11, 2019 5:03 AM
    Monday, November 11, 2019 2:19 AM