[UWP] How to change App.Current.Resources after installing Microsoft.UI.Xaml in C#?

Bel 46 Reputation points
2022-11-04T09:44:45.543+00:00

After Installing this package Microsoft.UI.Xaml

I always getting this error... and I can't change the App.Current.Resources dynamically in C#.
257168-screenshot-2022-11-04-173651.png

in my App.xaml

   xaml  
   <Application  
       x:Class="UniversalProject.UWP.App"  
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
       xmlns:controls="using:Microsoft.UI.Xaml.Controls"  
       xmlns:local="using:UniversalProject.UWP"  
       xmlns:uwpbasic="using:UWP.Basic.Controls">  
       <Application.Resources>  
           <controls:XamlControlsResources>  
               <controls:XamlControlsResources.MergedDictionaries>  
                   <ResourceDictionary>  
                   </ResourceDictionary>  
                   <ResourceDictionary Source="Styles/ApplicationStyles.xaml" />  
               </controls:XamlControlsResources.MergedDictionaries>  
           </controls:XamlControlsResources>          
           <!--<ResourceDictionary>  
               <ResourceDictionary.MergedDictionaries>  
                   <ResourceDictionary Source="Styles/ApplicationStyles.xaml" />  
               </ResourceDictionary.MergedDictionaries>  
           </ResourceDictionary>-->  
       </Application.Resources>  
   </Application>  

Please help me to change resources dynamically in C# with Microsoft.UI.Xaml

Universal Windows Platform (UWP)
Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
737 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bel 46 Reputation points
    2022-11-05T02:50:28.02+00:00

    Already fixed by accessing merge dictionary by index.

       xaml  
       <Application.Resources>  
               <controls:XamlControlsResources>  
                   <controls:XamlControlsResources.MergedDictionaries>  
                       <ResourceDictionary>  
                           <Color x:Key="SystemAccentColor">Blue</Color>  
                       </ResourceDictionary>  
                       <ResourceDictionary Source="Styles/ApplicationStyles.xaml" />  
                   </controls:XamlControlsResources.MergedDictionaries>  
               </controls:XamlControlsResources>  
           </Application.Resources>  
    

    var r = Application.Current.Resources;
    var Resources = r.MergedDictionaries[0];
    Resources["SystemAccentColor"] = SystemAccentSolidColor.Color;