当SolidColorBrush是什么时,为什么Silverlight Canvas ImageBrush不能作为CanvasBackground显示?

时间:2012-08-30 16:51:00

标签: silverlight background

我有两种非常简单的XAML变体。首先是使用纯色画笔绘制画布背景的变体。

<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>

这是托管在一个相当简单的HTML页面

<!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>

当我编译并打开页面时,我看到了我所期待的,一个空的海蓝宝石页面。但是,如果我更改XAML以使用ImageBrush代替SolidColorBrush,则页面为黑色。图像显示在设计视图中(可能是因为它具有设置时间设置的高度和宽度),但不在页面中。

<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>

1 个答案:

答案 0 :(得分:0)

密钥位于Silverlight项目中图像的“Build Action”属性中。我把它设置为'资源'并将其更改为'内容'解决了问题。