Thursday, August 30, 2012

Why isn't a Silverlight Canvas ImageBrush visible as the CanvasBackground when a SolidColorBrush is?

Why isn't a Silverlight Canvas ImageBrush visible as the CanvasBackground when a SolidColorBrush is?

I have two variations of an extremely simple piece of XAML. First here is the variant painting the background of a canvas with a solid colour brush.

<UserControl x:Class="CanvasBackground.MainPage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:Ignorable="d"     d:DesignHeight="300" d:DesignWidth="400">      <Grid x:Name="LayoutRoot">         <Canvas x:Name="SeedCanvas">             <Canvas.Background>                 <SolidColorBrush Color="Aquamarine" />             </Canvas.Background>         </Canvas>     </Grid> </UserControl> 

This is hosted into a fairly simple HTML page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>CanvasBackground</title> <style type="text/css">     html, body {         height: 100%;         overflow: auto;     }     body {         padding: 0;         margin: 0;     }     #silverlightControlHost {         height: 100%;         text-align:center;     } </style> </head> <body> <form id="form1" runat="server" style="height:100%"> <div id="silverlightControlHost">     <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">         <param name="source" value="ClientBin/CanvasBackground.xap"/>         <param name="background" value="transparent" />     </object> </div> </form> </body> </html> 

When I compile and open the page I see what I expect, an empty aquamarine page. However if I change the XAML to use an ImageBrush in place of the SolidColorBrush like this the page is black. The image shows in the design view (presumably because that has the height and width for design time set) but not in the page.

<UserControl x:Class="CanvasBackground.MainPage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:Ignorable="d"     d:DesignHeight="300" d:DesignWidth="400">      <Grid x:Name="LayoutRoot">         <Canvas x:Name="SeedCanvas">             <Canvas.Background>                 <ImageBrush ImageSource="/images/Autum.jpg" Stretch="Fill"/>             </Canvas.Background>         </Canvas>     </Grid> </UserControl> 

Answers & Comments...

Answer: 1

The key was in the property 'Build Action' of the image in the Silverlight project. I had it set to 'Resource' and changing it to 'Content' fixed the issue.

by : dumbledadhttp://stackoverflow.com/users/575530




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog