views:

224

answers:

2

Hi,

I have a Silverlight application that I am working on. I have a few different custom button styles that use a ContentTemplate to display an Image as a Button, such as:

<Style x:Key="CancelButtonStyle" TargetType="Button">
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Image Source="Assets/Images/Controls/cancel.png"/>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

cancel.png is a recently added image and it isn't showing. If I add a simple Image to a page with the source set to this image I get the underline in the XMAL design viewer with the warning

The file 'Assets/Images/Controls/cancel.png' is not part of the project or its 'Build Action' property is not set to 'Resource'

I've double checked and it's build action is set to 'Resource'. I've unloaded the project and viewed the csproj file and there is a build entry for this image, what have I missed? I haven't done anything different with this image that I have with other images in the same folder which work.

Update

If I use the following I can see the image at design time but not at runtime, any ideas?

<Image Source="Foo.Bar;component/Assets/Images/Controls/cancel.png" Stretch="None"/>
A: 

I have had similar issues before, I ended up ignoring the compiler warning that you have highlighted, and it just worked at runtime. Often, the issue for me was that, at compile time, the path to the image was being read from the directory of the XAML file that is generating the warning, which is wrong, but at runtime, the path is read from the control that is using the Style, which often just works.

Do you have your Style in a Resource Dictionary file or something? Try, just for a test, to embed the style inline of your button, see if it works.

Mark
Thanks for that. I tried adding the style inline rather than in a resource but got same issue. In the end I used the following to get it to work: Source="/Foo.Bar;component/Assets/Images/Controls/cancel.png"
Fermin
ah those little slashes are a big pain!
Mark
A: 

I solved the issue by using the following as the image source:

Source="/Foo.Bar;component/Assets/Images/Controls/cancel.png" 

Although I'm still unsure of the root of the problem.

Fermin