473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to reference a recourse file from a class in WPF

I have a WPF windows app and from code in Window.xaml.cs I create an
instance of a class whish is derived from Adorner. In this class I create
shapes and need to apply a style from a resource file named
'Dictionary1.xaml'.

Normally if all this code was just running in Window.xaml.cs I could use
this line:

cornerThumb.Style = (Style)this.FindResource("RoundThumbLineEnd");

and use this reference in the window's xaml:

<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml" />
</Window.Resources>

but of course, the Adorner class where I'm working has no idea where
Dictionary1.xaml is.

Any tips?

Thanks.
--
mo*******@noemail.noemail
Jan 17 '07 #1
2 1712
Hi moondaddy,

When finding a resource, it first checks the current element's Resources
collection (its resource dictionary). If the item is not found, it checks
the parent element, its parent, and so on until it reaches the root
element. At that point, it checks the Resources collection on the
Application object. If it is not found there, it finally checks a system
collection (which contains system-defined fonts, colors, and other
settings). If the item is in none of these collections, it throws an
InvalidOperationException.

Therefore you could simply put resources in App.xaml within
<Application.Resourcesand those resources will be shared by all your code
in the same assembly.

For ResourceDictionary defined in separate .xaml files, you could use
following code to load it:

ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(@"Dictionary1.xaml", UriKind.Relative);

Then you can use rd["resource_key"] to reference each resource.

Hope this helps.
Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Jan 17 '07 #2
Great Thanks Walter! This is what I was looking for.
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:Ow**************@TK2MSFTNGHUB02.phx.gbl...
Hi moondaddy,

When finding a resource, it first checks the current element's Resources
collection (its resource dictionary). If the item is not found, it checks
the parent element, its parent, and so on until it reaches the root
element. At that point, it checks the Resources collection on the
Application object. If it is not found there, it finally checks a system
collection (which contains system-defined fonts, colors, and other
settings). If the item is in none of these collections, it throws an
InvalidOperationException.

Therefore you could simply put resources in App.xaml within
<Application.Resourcesand those resources will be shared by all your
code
in the same assembly.

For ResourceDictionary defined in separate .xaml files, you could use
following code to load it:

ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri(@"Dictionary1.xaml", UriKind.Relative);

Then you can use rd["resource_key"] to reference each resource.

Hope this helps.
Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Jan 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mdh | last post by:
I am trying to learn the basics of MVC applications using a Tomcat infrastructure. I'm starting by building a simple application with: * a login.jsp page for a basic login form with a action...
4
by: Edward Diener | last post by:
I have a class Y in assembly B which is derived from a class Z in assembly C. So I correctly add a reference to assembly C in assembly B, build assembly B and everything builds fine. Now I create...
3
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page...
5
by: Mike Logan | last post by:
I used WSDL.exe to generate a client side web proxy for a web service, called the web service, got the results but an array returned by the web service is not in the results. However if I use...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
2
by: raylopez99 | last post by:
I'm having problems compiling complex reference declarations in MSVC++.NET 2002 IDE. Here is an example: // --Foo.h-- #include "Bar.h" class Bar; //forward decl. to a class Bar in...
2
by: ken | last post by:
Hi, i have a class: class LogHandler(ContentHandler): # a reference to a file open by some other function/class outputFile; def endElement(self, name): doSomething(self,...
9
by: Davidhere40 | last post by:
Do I have to use a web reference to access use a remote web reference? I can't seem to just use the proxy generated by my web services project, because that one never calls the remote service, its...
0
balabaster
by: balabaster | last post by:
Hi all, I'm creating a class factory and a bunch of adapters to allow in house applications to communicate with mobile applications over different cellular networks. I've got my class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.