views:

278

answers:

2

I am using Visual Studio 2010 RC1.

I define a resource "Brush2" in app.xaml_:

<Application x:Class="VideoThumbnails.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <RadialGradientBrush x:Key="Brush2" RadiusX="1" RadiusY="1" GradientOrigin="0.3,0.3">
            <GradientStop Color="White" Offset="0"/>
            <GradientStop Color="#ffc0c0" Offset="1"/>
        </RadialGradientBrush>

    </Application.Resources>
</Application>

In my Mainwindow I am trying to use that resource:

...
<Border Margin="4,2" BorderBrush="Black" BorderThickness="2" CornerRadius="4"
        ToolTip="{Binding Path=FullPath}" HorizontalAlignment="Stretch"
        Background="{StaticResource Brush2}">
...

No matter what I do it always raises an exception at runtime (Resource not found). I changed build action without success.

How can I use resources defined in app.xaml?

+1  A: 

Nothing you have done is incorrect. You either have 1) screwed up the project build somehow while randomly doing things to try to get it to work or 2) something else is going on here and we'll never know without the exception details.

I would highly suggest you try to repro this in a fresh brand new WPF project. Do the following steps (and ONLY the following steps):

Create a new WPF project, add the exact same brush to app.xaml, then open Window1 and bind the window's background to the resource. Run the app.

It should work as expected. If not, come back with the exception details. If it does, compare this new project with your current one to see what you are doing differently.

Will
Thanks to your suggestion it works finally. I explicitly set the startup object in project properties to a certain class I defined myself. I changed that to "(not set)" and it works. How strange...
DerKlaus
This actually makes sense. Not Set would lead to the App.xaml being interpreted to figure out what to do on startup. Setting it to your own class would result in that not happening unless you did it explicitly.
Ben Von Handorf
@DerKlaus - Thanks for your comment! I'd set App.xaml's build action to Page so I could create my own entry point. (this of course caused me to lose my shared resource dictionary from loading styles at design time). I did not know I could set the startup object in the project properties until reading your comment. This allowed me to set App.xaml's build action back to Application Definition! Thanks again!
Scott
A: 

Should be using DynamicResource instead of StaticResource

Matt