如何在 ASP.NET 面板中更改 Silverlight 应用程序的高度?
如何动态更改嵌入在 ASP.NET 面板中的 Silverlight 应用程序的高度?
ASP.NET 面板标记
<asp:Panel ID="pnlResult" runat="server" Visible="false">
<br />
<div class="cpHeader">
<asp:Label ID="Label1" runat="server" Text="Search Results"></asp:Label>
</div>
<asp:Panel ID="Panel3" runat="server" class='cpResultBody'>
</asp:Panel>
</asp:Panel>
我将 Silverlight 动态添加到上面的 ASP.NET 面板 Panel3
中,如下所示: (这样做是为了动态传递 initparams)
HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");
myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";
myHtmlObject.Attributes["type"] = "application/x-silverlight-2";
HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApp.xap";
myHtmlObject.Controls.Add(mySourceParam);
HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
HtmlGenericControl myInputParam = new HtmlGenericControl("param");
myInputParam.Attributes["name"] = "initparams";
myInputParam.Attributes["value"] = string.Format("param1={0},param2={1},param2={2}", param1.ToString(), param2.ToString(), param3.ToString());
myHtmlObject.Controls.Add(myInputParam);
myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
Panel3.Controls.Add(myHtmlObject);
最后我的 MainPage.xaml 如下所示。这里我不设置高度。
<UserControl x:Class="MySilverlightApp.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"
xmlns:my="clr-namespace:MySilverlightApp"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="WhiteSmoke" >
<Canvas Name="canvas1" HorizontalAlignment="Left" Margin="-1,-1,0,0"
VerticalAlignment="Top"/>
</Grid>
</UserControl>
我的应用程序是这样的,一些数据在 Canvas
canvas1
中表示为图形,其高度无法事先知道。
截至目前,silverlight 应用程序正在受到限制。如何避免这种情况并确保高度根据内容按比例放大。
观察:如果我手动将 silverlight 对象创建代码中的 Height
从 100%
更改为 1500px
,高度似乎会增加一点,但是即使我进一步增加高度,内容仍然会被剪裁。
How can I dynamically change height of Silverlight application embedded in ASP.NET panel?'
ASP.NET Panel markup
<asp:Panel ID="pnlResult" runat="server" Visible="false">
<br />
<div class="cpHeader">
<asp:Label ID="Label1" runat="server" Text="Search Results"></asp:Label>
</div>
<asp:Panel ID="Panel3" runat="server" class='cpResultBody'>
</asp:Panel>
</asp:Panel>
I dynamically add the Silverlight to the above ASP.NET panel, Panel3
as shown below:
(This is done to pass initparams dynamically)
HtmlGenericControl myHtmlObject = new HtmlGenericControl("object");
myHtmlObject.Attributes["data"] = "data:application/x-silverlight-2";
myHtmlObject.Attributes["type"] = "application/x-silverlight-2";
HtmlGenericControl mySourceParam = new HtmlGenericControl("param");
mySourceParam.Attributes["name"] = "source";
mySourceParam.Attributes["value"] = "ClientBin/MySilverlightApp.xap";
myHtmlObject.Controls.Add(mySourceParam);
HtmlGenericControl myOnErrorParam = new HtmlGenericControl("param");
HtmlGenericControl myInputParam = new HtmlGenericControl("param");
myInputParam.Attributes["name"] = "initparams";
myInputParam.Attributes["value"] = string.Format("param1={0},param2={1},param2={2}", param1.ToString(), param2.ToString(), param3.ToString());
myHtmlObject.Controls.Add(myInputParam);
myHtmlObject.Attributes["width"] = "100%";
myHtmlObject.Attributes["height"] = "100%";
Panel3.Controls.Add(myHtmlObject);
Finally my MainPage.xaml is as below. Here I am not setting height.
<UserControl x:Class="MySilverlightApp.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"
xmlns:my="clr-namespace:MySilverlightApp"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Grid x:Name="LayoutRoot" Background="WhiteSmoke" >
<Canvas Name="canvas1" HorizontalAlignment="Left" Margin="-1,-1,0,0"
VerticalAlignment="Top"/>
</Grid>
</UserControl>
My application is such that some data is represented as figures in the Canvas
canvas1
, the height of which cannot be known beforehand.
As of now the silverlight application is getting clipped. How to avoid this and ensure that the height is scaled up based on the content.
Observation: If I manually change the Height
in silverlight object creating code from 100%
to 1500px
, the height seems to increase a bit more, but still contents are clipped even if I increase heigh further.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不是 Silverlight 方面的专家,但我对这个问题的快速反应是,修复 Grid(layoutroot) 的高度并允许滚动(水平和垂直,以数据被剪切的方式为准),然后在中提供固定高度的 silverlight 对象HTML。
I am not much of expert on Silverlight but my quick response to this problem would be, fix the height of Grid(layoutroot) and allow scrolls(horizontal and vertical, whichever is way data is getting clipped) and then provide fixed height silverlight object in HTML.
您可以这样做:当 Silverlight 控件加载到浏览器中(Loaded 事件)时,向浏览器发送一个命令,以增大该控件所在的 div 以匹配该控件的 ActualHeight。 (Vb.net)
这是脚本:
GrowDiv: function(divheight) {
//警报(div高度);
jQuery("body").css("高度", divheight);
jQuery("#silverlightControlHost").css("高度", divheight);
jQuery("#Panel3").css("高度", divheight);
}
}
You could do this: when the Silverlight control is loaded in the browser (Loaded event) send a command to the browser to grow the div that the control is hosted in to match the ActualHeight of the control. (Vb.net)
this is the script:
GrowDiv: function(divheight) {
//alert(divheight);
jQuery("body").css("height", divheight);
jQuery("#silverlightControlHost").css("height", divheight);
jQuery("#Panel3").css("height", divheight);
}
}