Replacing merged resource dictionaries when loading a Silverlight application

I have a set of styles and brushes defined in the ResourceDictionary that I load as MergedDictionary in the XAML of my top-level control:

<ResourceDictionary.MergedDictionaries>
     <ResourceDictionary Source="/MyAssembly;component/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>

I try to replace some of these styles and brushes if necessary, if another XAML file with its own ResourceDictionary exists in XAP. I am trying to combine this dictionary at runtime before InitializeComponent () is called in my user control. For this, I use the following code:

public static class StyleLoader
{
    public static void MergeStyle(string xamlUri)
    {
        try
        {
            XDocument xaml = XDocument.Load(xamlUri);
            ResourceDictionary rd = XamlReader.Load(xaml.ToString()) as ResourceDictionary;
            Application.Current.Resources.MergedDictionaries.Add(rd);

        }
        catch (XmlException ex)
        {
            // if the file doesn't exist, we can't add it
        }
    }
}

, , , . XAML , :

    StyleLoader.MergeStyle("/MyAssembly;component/Styles.xaml");
    StyleLoader.MergeStyle("BrushReplacements.xaml");

    InitializeComponent();

, XAML Blend. - , , Blend, ? !

+3

Source: https://habr.com/ru/post/1726092/


All Articles