14

我希望我的 Silverlight 应用程序填满整个浏览器窗口。我已将插件对象的宽度和高度设置为 100%,并将我的 LayoutRoot 容器的高度和宽度设置为自动,但仍然没有运气。有什么建议么?

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:Silverlight ID="Silverlight1" runat="server" 
        Source="~/ClientBin/Client.xap"
        MinimumVersion="2.0.30818.0"
        AutoUpgrade="true"
        Height="100%" 
        Width="100%">
    </asp:Silverlight>
</form>

<UserControl 
    x:Class="Client.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="Auto"
    Width="Auto">
    <Grid 
        x:Name="LayoutRoot" 
        Background="#084E85"
        ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="280" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="600" />
        </Grid.RowDefinitions>

        ...Remaining content here...
    </Grid>
</UserControl>

免责声明:我首先搜索了答案,找到了这个帖子。但是,正如您从我的代码中看到的那样,它对我不起作用。

4

4 回答 4

18

首先,我没有在用户控件中设置高度/宽度。相反,我设置了 DesignHeight 和 DesignWidth(在 " http://schemas.microsoft.com/expression/blend/2008" 命名空间中)并将对齐设置为拉伸

<UserControl ... 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
    d:DesignHeight="1050" d:DesignWidth="1680">

在我的 HTML 中,我将高度和宽度设置为 100%,如下所示...

<div style="height: 100%; width: 100%; position: fixed;">
        <asp:Silverlight runat="server" Source="~/ClientBin/My.xap" ID="MyId" 
            Width="100%" Height="100%" />
</div>

那时,一切都对我有用,可以占据整个窗口。

于 2009-04-07T18:23:15.087 回答
0

从 中删除 Height 和 Width 属性:

<UserControl
     x:Class="Client.Page"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

自动高度和宽度调整窗口大小以适应内容。如果您删除 Height 和 Width 属性,您的 Silverlight 应用程序将增长以适合窗口。

于 2009-04-07T18:22:35.057 回答
0

我认为您的 Grid 设置得尽可能小(没有“星”列或行)。您可以尝试使用“星号”大小的多列和行吗?

正如其他人指出的那样,您还需要删除“自动”大小,因为它们会根据内容自动调整大小。

还尝试在页面上设置背景颜色,以便您看到它实际扩展的位置。

于 2009-04-07T18:23:46.973 回答
0

如果您有一个相当复杂的 HTML 布局并且不能依赖固定定位,那么您可以使用以下命令调整 #silverlightControlHost div 的大小:

private bool _hasResized;

protected override Size ArrangeOverride(Size finalSize)
{
    if (!_hasResized)
    {
        HtmlPage.Document.GetElementById("silverlightControlHost").SetStyleAttribute("height", finalSize.Height.ToString());
        _hasResized = true;
    }

    return base.ArrangeOverride(finalSize);
}

你可以把它放在里面MainPage.cs,或者如果你有嵌套UserControls,那么需要最高高度的控件。我还使用以下 XAML 和 Visual Studio 提供的默认 HTML

<UserControl ...
    VerticalAlignment="Stretch"
    Height="Auto"
    Width="Auto">

没有这些设置我还没有测试过,据我所知 Auto 是默认设置。

于 2010-05-20T14:04:12.763 回答