wpf - how to use one resource definition in many places as identical objects

R

Rolf Welskes

Hello,
because there are no managed news group for window presentation foundation
and one
has told me I can use this, here my problem:

I have one resource-definition,
for example in an own xaml file, a large number of objects defined as
resources.

Now I need these resources (objects) in many windows, controls, etc. in one
DLL.
I cannot do the resources on Application-Level, because they should not be
availabe in a large program - only in the one DLL.

So I have tried the following:
I have defined the resources in an own xaml file.
Now I have used ResourceDictionary.MergedDictionies and in all files so
added the resources.
This works.
BUT: now I have seen, that the resources are re-constructed in all - for
example 30 windows.
Means if I have for example a Brush in resources declared as:
<SolidColorBrush Color="Red", x:Key="myRedBrush"/>
and use MergedDictionay in 30 window.xaml-files, I have 30 Brushes, not only
one.

Simple would be to put this resources in Application then it would be
availabe at all places unique.
But this is not possible if you have a large Application of hundrets of
parts (DLLs).

So is there a solution to define reources one time, use it at many places,
for example in 30 window.xaml-files,
but the resources are unique, means each object of the resources is
constructed only one time and used many times.

Thank you for any help.
Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

I'm not sure if I fully understood your question, so please feel free to
correct me if I misunderstood anything.

Suppose you have a resource only class library (name it as
MySharedResources), and you have a ResourceDictionary1.xaml in it:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="myBrush" Color="Blue"></SolidColorBrush>
</ResourceDictionary>

You can add reference to this class library in your 30 WPF assemblies and
share the ResourceDictionary1.xaml:

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="/MySharedResources;component/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>


<Grid>
<Button Background="{StaticResource myBrush}">Hello</Button>
</Grid>


More information about the resource pack uri used to reference a resource
from a referenced assembly:

#Pack URIs in Windows Presentation Foundation
http://msdn2.microsoft.com/en-us/library/aa970069.aspx
<quote>
The following example shows the pack URI for a XAML resource file that is
located in the root folder of a referenced, version-specific assembly's
project folder:

pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xa
ml
</quote>


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you for your informations,
but exactly in this way I have done the work.
But,
try the following:
Do the resource reference in three windows in the same kind,
allways reference by MergeDictionaries the same external dictionary.
You get all objects three time.

You can see this if you get the same object from the resource say obj01 from
window01, window02, window03,
all are different objects.

Think this would be thousend of obects in a resource, used 100 time so you
have 99000 objekt you do not need in memory.

Reference should be unique.
means:
I have obj01 in an myResource.xaml file as a resource.
Now I would use it in 10 windows by reference this resource I should allways
get the same obj01 not 10 different objects.

So I do not know how to do this.

Best Regards
Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

Please refer to following blog:

#Windows Presentation Foundation SDK : Defining and Using Shared Resources
in a Custom Control Library
http://blogs.msdn.com/wpfsdk/archive/2007/06/08/defining-and-using-shared-re
sources-in-a-custom-control-library.aspx

I've tested first approach in the blog which is using a static property to
return a resource singleton and add that resource to your control's
Resources.MergedDictionaries before InitializeComponent(). Although the
sample code in the blog uses two controls in the same project as the
resource, if you make the static property public and accessible to other
assemblies, they will be able to use the same resource too.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
thank you for your work.
I have tried it and it works fine when running the program.
BUT:
It is no usable because all my xaml files do not work in the Visual Studio
Designer.

Means when I refer in xaml to an resource key for example myKey of a
resource
and the resource is definied in the external resource dictionay
you get an resourrce key not found error from the VS-Xaml-Designer.
The xaml-designer does not run the constructor of the window class, where
the add to
the resourcedictionary is coded.

Also the sample for control devolopers is very weak, because think you want
to make
for example 5 usercontrols - these controls are definied in xaml in a
myUserControl.DLL
How to use shared resources in this case?

It's a hard problem I think.

Thank you for any help.

Best Regards
Rolf Welskes
 
W

Walter Wang [MSFT]

Hi Rolf,

Thanks for your follow-up.

For first question, I think this is indeed a limitation of WPF's current
implementation. I'm afraid there's no better way to workaround this issue.
Please feel free to submit your feedback here
http://connect.microsoft.com/Main/content/content.aspx?ContentID=2220.

For your second question, I'm not sure if I've fully understood it. Would
you please depict more? Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rolf Welskes

Hello,
the second question is the same as for the first, but only relative to
controls.
So I think we have solved this in this sence, more is not possible in the
moment.

I think one solution will be to add the external dictionary in xaml for each
window or each control,
so xamls, VS and Designer all works fine.
When running the program one removes the dictionary and replaces it with the
unique one
as in described in the solution of the probem we have.

So we have a working VS-Designer on the one hand and the resourcees only
once when the program is running.
I will check this in the next days.

So I think this case is closed.

Thank you for your work and best regards
Rolf Welskes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top