我有一个Silverlight应用程序,我希望允许像常规HTML页面一样大小。也就是说,我希望silverlight插件的大小能够在高度上扩展和收缩,以适应我填充Silverlight应用程序的动态数据和控件。所以,假设我有一个带有网格和按钮的页面。用户点击按钮,网格获得一堆行,图像使网格的自然大小高于浏览器窗口。我想有一些东西来调整silverlight插件的大小以适应网格的DesiredSize。以下以及其他一些尝试都无效:
// handler in my main page.
void MainGrid_LayoutUpdated(object sender, EventArgs e)
{
HtmlPage.Window.Invoke("setSilverlightSize", this.MainGrid.DesiredSize.Height);
}
<body style="height:100%;margin:0;">
<form id="mainForm" runat="server" style="height:100%;">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<div id="SilverlightContainer" style="height:100%;">
<asp:Silverlight ID="SLMain" runat="server" Source="~/ClientBin/My.Silverlight.Main.xap" Version="2.0" Width="100%" Height="100%" />
</div>
</form>
<script type="text/javascript">
function setSilverlightSize(val) {
var host = document.getElementById("SilverlightContainer");
host.style.height = val + "px";
}
</script>
</body>
MainGrid的所需大小始终希望是窗口的大小。阿格说海盗。
-r
答案 0 :(得分:2)
这应涵盖所有基础。请参阅下面的HTML示例。
试试这个:
<style type="text/css">
html, body { overflow:auto }
</style>
<head>
<title>My App</title>
</head>
<body id="bodyId" style="margin:0;" scroll="no">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="position: fixed; height: 100%; width: 100%">
<avn:Silverlight
ID="xamlHost"
runat="server"
Source="~/ClientBin/THE.xap"
MinimumVersion="x.x.xxxxx"
Width="100%"
Height="100%"
/>
</div>
</form>
</body>
</html>
答案 1 :(得分:1)
我用过这个,似乎效果很好。需要获得渲染“ed”大小。
private void LayoutRoot_LayoutUpdated(object sender, System.EventArgs e)
{
// Set the size of the SL object to the new rendered size of the Grid.
ResizeSilverlightOnject(this.LayoutRoot.RenderSize.Height);
}
private void ResizeSilverlightObject( double height )
{
HtmlPage.Window.Invoke( "ResizeObject", new object[] { height } );
}
答案 2 :(得分:1)
在我的情况下,我创建了一个事件,并将高度数据发送到浏览器以处理来自javascript的高度值 这是我的HTML代码:
/*
When Silverlight is loaded attach silverlight events
*/
function OnSilverlightLoaded(sender, args) {
//Getting Silverlight object on page
_silverlightControlHost = document.getElementById("SilverlightObject");
//Subscribe to it events
_silverlightControlHost.Content.SilverlightApp.SetHeightEvent = SetHeightEvent;
}
这是我的Silverlight代码:
[ScriptableType]
public partial class MainPage
{
//Register the event to be as accessible from script
[ScriptableMember]
public event EventHandler<HeightEventHandler> SetHeightEvent;
public MainPage()
{
InitializeComponent();
//Register this instance to be accessible from script
HtmlPage.RegisterScriptableObject("SilverlightApp", this);
}
public void SomeMethod()
{
if (this.SetHeightEvent != null)
this.SetHeightEvent(this, new HeightEventHandler() { Altura = _heightSilverObj.ToString() });
}
}
[ScriptableType]
public class HeightEventHandler : EventArgs
{
public string Altura { get; set; }
}
这很有用!
答案 3 :(得分:0)
如果您尝试使用验证摘要确定高度(与此问题略有不同,但很有用),请尝试以下操作: 在Page.xaml.cs中 //将原始高度存储在Page_Loaded事件
中void Page_Loaded(object sender, RoutedEventArgs e)
{
SetupPage();
this.ValidationSummary.SizeChanged += new SizeChangedEventHandler(ValidationSummary_SizeChanged);
}
void ValidationSummary_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (this.OriginalPageHeight == 0.0)
{
this.OriginalPageHeight = this.RenderSize.Height;
}
else
{
double expandSize = this.OriginalPageHeight + e.NewSize.Height;
ResizeSilverlightObject(expandSize);
}
}
编辑:添加了Javascript函数和cs函数
function ResizeObject(height) {
var host = document.getElementById("uploaderHost");
host.style.height = height + "px";
}
public void ResizeSilverlightObject(double height)
{
HtmlPage.HtmlWindow.Invoke("ResizeObject", new object[] { height });
}