Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Resources doesn't work at all?

 
 
Old 04-28-2006   #1 (permalink)
HokutoNoKen


 
 

Resources doesn't work at all?

Hi,

In the book WPF (O'REILLY) they have a simple example looking like this.

<Window x:Class="....

>


<Window.Resources>
<SolidColorBrush x:Key="Foo" Collor="Green" />
...
</Window.Resources>

<Grid Name="myGrid"> <!-- I have tried x:Name="myGrid" -->
</Grid>
</Window>

Code-Behind:

Brush b = (Brush)myGrid.FindResource("Foo"); <!-- This will not work, it
will throw an exception. -->

I have tried following things too.

Brush b = (Brush)this.Resources["Foo"]; <!-- Will not throw exception but
return value will be Null -->
Brush b = (Brush)this.FindResource("Foo"); <!-- Will throw exception -->

/Please help



My System SpecsSystem Spec
Old 04-28-2006   #2 (permalink)
viliescu


 
 

RE: Resources doesn't work at all?

Where in the code-behind did you try?
The line Brush b = (Brush)this.FindResource("Foo"); should work,
but maybe you have tried this in the constructor. Try with the code in the
Window's Loaded event.
--
Valentin Iliescu [MVP - Client Application Development]


"HokutoNoKen" wrote:

> Hi,
>
> In the book WPF (O'REILLY) they have a simple example looking like this.
>
> <Window x:Class="....
>
> >

>
> <Window.Resources>
> <SolidColorBrush x:Key="Foo" Collor="Green" />
> ...
> </Window.Resources>
>
> <Grid Name="myGrid"> <!-- I have tried x:Name="myGrid" -->
> </Grid>
> </Window>
>
> Code-Behind:
>
> Brush b = (Brush)myGrid.FindResource("Foo"); <!-- This will not work, it
> will throw an exception. -->
>
> I have tried following things too.
>
> Brush b = (Brush)this.Resources["Foo"]; <!-- Will not throw exception but
> return value will be Null -->
> Brush b = (Brush)this.FindResource("Foo"); <!-- Will throw exception -->
>
> /Please help
>
>
>

My System SpecsSystem Spec
Old 05-04-2006   #3 (permalink)
Tommy Larsson


 
 

Re: Resources doesn't work at all?

Add loaded on the window , see below and the eventhandler Windowloaded in
the code behind

Window1.xaml :

<Window x:Class="WindowsApplication8.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="WindowsApplication10" Height="300" Width="300"

Loaded="WindowLoaded"

>


<Window.Resources>

<Ellipse x:Key="shape" Fill="Blue" Width="100" Height="80" />



</Window.Resources>

<DockPanel x:Name="myDockPanel"></DockPanel>

</Window>



Window1.xaml.cs:

private void WindowLoaded(object sender, RoutedEventArgs e)

{

// Add button

Button myButton = new Button();

myButton.Content = "Click me!";

this.myDockPanel.Children.Add(myButton);

Ellipse myEllipse = (Ellipse)this.FindResource("shape");

this.myDockPanel.Children.Add(myEllipse);

}


Regards,
Tommy

"HokutoNoKen" <tommy.herceg@programgruppen.se> wrote in message
news:%23Tys9NsaGHA.3736@TK2MSFTNGP04.phx.gbl...
> Hi,
>
> In the book WPF (O'REILLY) they have a simple example looking like this.
>
> <Window x:Class="....
>
>>

>
> <Window.Resources>
> <SolidColorBrush x:Key="Foo" Collor="Green" />
> ...
> </Window.Resources>
>
> <Grid Name="myGrid"> <!-- I have tried x:Name="myGrid" -->
> </Grid>
> </Window>
>
> Code-Behind:
>
> Brush b = (Brush)myGrid.FindResource("Foo"); <!-- This will not work, it
> will throw an exception. -->
>
> I have tried following things too.
>
> Brush b = (Brush)this.Resources["Foo"]; <!-- Will not throw exception but
> return value will be Null -->
> Brush b = (Brush)this.FindResource("Foo"); <!-- Will throw exception -->
>
> /Please help
>



My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Not enough resources Vista hardware & devices
resources Vista hardware & devices
PCI to PCI Bridge not enough resources Vista hardware & devices


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46