Personally, I prefer to use App.xaml
or seperate ResourceDictionary
over adding them all to the Window.Resources
, this eliminates clutter in your Window
xaml.
This also allows you to easily create Themes
for you application as you have them all in one place, so you can copy your existing ResourceDictionary
change brush colors etc, the you can choose what ResourceDictionary
you want to load and and easily change the entire apperance of your application.
As for accessing the Resouces
in your Usercontrol
there is no difference on the xaml side, you will continue to use {StaticResource resourceKey}
as when you call a StaticResource
it searches though the Resource
hierarchy to find the Resource
.
So if you move you resource from UserControl.Resources
to Window.Resources
or Application.Resources
you don't need to change anything in your xaml code that is accessing though {StaticResource resourceKey}
.
As for access in code behind you will use FindResource("resourceKey")
instead of Resources["resourceKey"]
as FindResource
will search the hierarchy for the Resource
like the StaticResource
does in xaml.
Example:
m_AnimationHide = (Storyboard)FindResource("AnimationHide");
If you want to modify any of these Resources in a specific control and they are frozen you can just create a copy for that instance
Example
var animation = FindResource("AnimationHide") as Storyboard;
m_AnimationHide = animation.Clone();
m_AnimationHide.Completed += m_AnimationHide_Completed;
You can also set x:Shared="false"
this will return a new instance of the animation each time from the Resources, this will save copy/pasting the same animation througout your application if you have a complex Resource that you need to change values in.
<Storyboard x:Key="AnimationHide" x:Shared="false" />
Then you will be able to modify the resource locally.