antsonthetree
Topic Author
Posts: 44
Joined: 09 Jan 2018, 15:56

Unity Modify ResourceDictionary at Runtime

03 Mar 2018, 20:25

Hello,
I have the code below that works fine in Blend - the strings are all updated when the button is clicked, but does not work with Noesis/Unity. Is this currently supported? The idea is to change the language strings at runtime.
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary x:Name="ThemeDictionary">
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Strings-EN.xaml"/>
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
        <ResourceDictionary Source="Styles/Res_MainMenu.xaml"/>
      </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
  </UserControl.Resources>
  
 public ResourceDictionary ThemeDictionary
        {
            // You could probably get it via its name with some query logic as well.
            get { return (ResourceDictionary) Resources.MergedDictionaries[0]; }
        }

        public void ChangeTheme()
        {

#if NOESIS
            Debug.Log(((ResourceDictionary)ThemeDictionary.MergedDictionaries[0]).Source);
            string uri = "Assets/NoesisGUI/Styles/Strings-ES.xaml";
            
            //ThemeDictionary.MergedDictionaries.Clear();
            //ThemeDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });
            ((ResourceDictionary)ThemeDictionary.MergedDictionaries[0]).Source = uri;
            Debug.Log(((ResourceDictionary)ThemeDictionary.MergedDictionaries[0]).Source);
#else
            Console.WriteLine(ThemeDictionary.MergedDictionaries[0].Source.ToString());
            System.Uri uri = new System.Uri("Assets/NoesisGUI/Styles/Strings-ES.xaml", System.UriKind.Relative);
            ThemeDictionary.MergedDictionaries.Clear();
            ThemeDictionary.MergedDictionaries.Add(new ResourceDictionary() { Source = uri });

            //Uri uri = new Uri("Styles/Strings-ES.xaml", UriKind.Relative);
            //ThemeDictionary.MergedDictionaries[0].Source = uri;
            Console.WriteLine(ThemeDictionary.MergedDictionaries[0].Source.ToString());
#endif
        }
 
User avatar
sfernandez
Site Admin
Posts: 1973
Joined: 22 Dec 2011, 19:20

Re: Unity Modify ResourceDictionary at Runtime

06 Mar 2018, 17:54

Hi,

It seems to be a bug in the MergedDictionaries change notifications.
Could you please create a ticket in our bugtracker attaching that sample?

Thanks.

Who is online

Users browsing this forum: No registered users and 6 guests