Il y a deux façons de le faire: soit en accédant à l'élément DOM directement et changer son style (ou css) attributs, ou en appelant une fonction javascript sur la page qui fera la même chose. Ci-dessous j'ai le xaml, le code derrière, et le HTML pour un exemple simple qui lorsque vous faites glisser un curseur dans le contrôle silverlight, il redimensionne la div qui contient le contrôle. Si vous créez une application Silverlight simple avec un site Web et une page de test complémentaires, puis copiez et collez le code suivant, vous pouvez avoir une lecture (notez que j'ai extrait certains styles/scripts de la page aspx pour le plaisir de concision).
Le code C# et javascript n'est pas particulièrement joli ou à l'épreuve des balles, c'est simplement un exemple.
<UserControl x:Class="SilverlightApplication6.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:DesignWidth="200" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Slider x:Name="WidthSlider" Value="50" Maximum="200"></Slider>
</Grid>
</UserControl>
code derrière l'application Silverlight:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
WidthSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(WidthSlider_ValueChanged);
}
private void WidthSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
//access and manipulate the DOM element directly:
HtmlElement container = HtmlPage.Document.GetElementById("silverlightControlHost");
if (container != null)
{
container.SetStyleAttribute("width", (50 + e.NewValue).ToString() + "px");
}
//pass a delta value to the js function, which will get added to the current width of the container:
HtmlPage.Window.Invoke("resizeContainer", (e.NewValue - e.OldValue).ToString());
}
}
et la page ASPX:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!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 runat="server">
<title>SilverlightApplication6</title>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
function resizeContainer(delta) {
var container = document.getElementById('silverlightControlHost');
if (container != null) {
//alert('starting width: ' + container.style.width);
container.style.width = (parseInt(container.style.width) + Number(delta) + 'px');
//alert('finishing width: ' + container.style.width);
}
}
</script>
</head>
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost" style="border: solid 1px red; width:200px; height: 400px;">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/SilverlightApplication6.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
</body>
</html>
Edit: Deux jours après avoir écrit cette réponse Charles Petzold a écrit un billet de blog sur le redimensionnement des contrôles Silverlight dans la page html, you can find it here. La principale différence est qu'il redimensionne le contrôle plugin Silverlight réel, alors que je redimensionnais l'élément html que le plugin Silverlight réside à l'intérieur.
Pas tout à fait ce que je cherchais, merci cependant. Mes utilisateurs souhaitent que le contrôle Silverlight s'agrandisse et défile uniquement avec les barres de défilement du navigateur. Donc, j'ai besoin d'obtenir la hauteur de l'application SL et de la transmettre au navigateur en quelque sorte. – Gus