我有两个非常简单的 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>