Hello star-italia and Tom,
XamlReader.Load() can do the task when the resource dictionary is in a
loose XAML form. However, if the resource dictionary is compiled into
BAML, we need to use the Application.LoadComponent() method to de-serialize
it into ResourceDictionary object, then add it to either
Application.Current.Resources or Window.Resources dictionary through the
MergedDictionaries property:
ResourceDictionary resourceDictionary = Application.LoadComponent(new
Uri("CommonResources.xaml", UriKind.Relative)) as ResourceDictionary;
if (resourceDictionary != null)
{
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}
When the resource is merged into the Application level, we use
StaticResource or DynamicResource to refer to them in either Window1.xaml
or Widnow2.xaml files.
The code above can also be written in Window1 or Window2 constructor at
codebehind as follows:
ResourceDictionary resourceDictionary = Application.LoadComponent(new
Uri("CommonResources.xaml", UriKind.Relative)) as ResourceDictionary;
if (resourceDictionary != null)
{
this.Resources.MergedDictionaries.Add(resourceDictionary);
}
In this way, the window will have a separate copy of the resource
dictionaries, though this might not be very efficient considering the
consumption of the memory.
If you plan to merge resources completely in XAML, we can directly specify
the MergedDictionary in XAML as follows:
<App.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack uri to the resource dictionary xaml
file"/>
</ResourceDictionary.MergedDictionaries
</ResourceDictionary>
</ App.Resources>
One caveat here is that you define the Visuals like Button in the resource
dictionary as follows:
<Button x:Key="button"/>
Because one Visual can have only one visual parent, we cannot re-parented
it for multiple times in the visual tree, so that if the "button" resource
is going to be reused in multiple places, please specify the x:Shared
property to "False" when declaring the Button resource as this:
<Button x:Shared="False" x:key="button"/>
x:Shared means that, every time this "button" resource is applied, WPF will
create a unique copy. But when x:Shared is specified, resource dictionary
should be compiled into BAML beforehand. So if you are using loose,
non-compiled XAML, please ignore my suggestion.
Regards,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.