I've just removed a big memory issue for me, I used to merge our "Themes" resource dictionary in every xaml file instead of just in the app.cs.xaml.
However, after removing the merging in every file except the App.cs.xaml I've lost the design time styles/templates.
Please note: This only applies to the styles merged into our Themes.xaml (e.g. Color.xaml, Brushes.xaml - we have one for each type of style). Stuff defined directly in Themes.xaml (which we have none of..) works.
I see two solutions,
1) Have the merging commented out in XAML and just un-comment it when I want to work with the designs.
2) Have this in the default ctor of every control: (perhaps only works in Blend)
#if DEBUG
Resources.MergedDictionaries.Add(
new ResourceDictionary()
{
Source = new System.Uri(@"RD.xml")
}
);
#endif
There has to be a better way to get design time editing of pages and controls, anyone know?
Thank you!
What I do is add a class that inherits from ResourceDictionary and override the source property to check if IsInDesignMode is true.
If it is I set the source otherwise I leave the source blank (which effectivley prevents the dictionary from being merged at runtime)
public class BlendMergedDictionary : ResourceDictionary
{
public bool IsInDesignMode
{
get
{
return (bool)DependencyPropertyDescriptor.FromProperty(
DesignerProperties.IsInDesignModeProperty,
typeof(DependencyObject)
).Metadata.DefaultValue;
}
}
public new Uri Source
{
get { return base.Source; }
set
{
if (!IsInDesignMode)
return;
Debug.WriteLine("Setting Source = " + value);
base.Source = value;
}
}
}
Now when I need to reference the dictionary in Blend I merge in the dictionary like this
<ResourceDictionary.MergedDictionaries>
<BlendHelpers:BlendMergedDictionary Source="Foo.xaml" />
</ResourceDictionary.MergedDictionaries>
You still have to "merge" in the dictionary in every file, but you don't pay the penalty of actually loading the dictionary at runtime. The merge is only there to support design time behavior.