【问题标题】:Resize StackPanel to fit screen when removing ads移除广告时调整 StackPanel 的大小以适应屏幕
【发布时间】:2014-08-15 21:47:54
【问题描述】:

我有一个在屏幕底部有广告的 Windows Phone 8 应用。

当我删除广告时,我希望将其占用的区域替换为游戏屏幕内容。我已经搜索过这个,但还没有找到解决方案,我确定这是我错过的一些简单的东西!

例如,请看下面的代码:

<!--Panorama item one-->
<phone:PanoramaItem Header="Level Stastics">
    <Grid Margin="0,0,0,88"  >
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Height="Auto" Grid.Row="0" Margin="0,0,0,-30" >
            <Button x:Name="button1" Content="Click Me" Click="button1_Click"/>
            <Button Content="last Button remove" Click="Button_Click"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button Content="Button"/>
            <Button x:Name="last" Content="last Button"/>
        </StackPanel>
    </Grid>
</phone:PanoramaItem>

(不是我的实际代码,只是一个示例,看看如何删除组件并填充留下的空间)

当我单击 button1 时,它会删除最后一个按钮,但不会调整剩余按钮的大小以占用空间。 on_click 方法将按钮的可见性设置为折叠。我认为这会将其从可视化树中删除?

我想要的是有一个充满控件的游戏区域(其中我有 2 个,一个纵向页面和一个全景页面)和一个位于屏幕底部的区域作为广告空间 (480x80) .当用户购买完整产品时,广告被移除,广告上方的游戏空间占据全屏空间并自行调整大小。这可能吗?

【问题讨论】:

    标签: xaml windows-phone-8 user-controls


    【解决方案1】:

    这里需要注意两点:

    1. StackPanel 位于高度为“自动”的网格的第一行。当它处于“自动”状态时,它不允许它的内容使用剩余空间。它只是为内容的高度提供空间。不止于此。因此,将该行的高度设为 * 并将第二行的高度设为自动(如果不使用,则将其删除)。如果第二行未设置为自动,则两行将共享相等的空间/高度。

    2. StackPanel 不会将其内容传播到所有可用空间。它类似于网格“自动”。因此,将其替换为 Grid 并添加具有 * 作为高度的行,最后一行除外。最后一行的高度应该是自动的,因为它必须被折叠(* 总是为行分配空间的分割百分比)。

    试试这个:

    <Grid>
    
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    
    <Grid Height="Auto" Grid.Row="0" >
    
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="auto" />
        </Grid.RowDefinitions>
    
        <Button Grid.Row="0" x:Name="button1" Content="Click Me" Click="button1_Click" />
        <Button Grid.Row="1" Content="last Button remove" Click="Button_Click" />
        <Button Grid.Row="2" Content="Button" />
        <Button  Grid.Row="3" Content="Button" />
        <Button Grid.Row="4" Content="Button" />
        <Button Grid.Row="5" Content="Button" />
        <Button Grid.Row="6" Content="Button" />
        <Button Grid.Row="7" x:Name="last" Content="last Button" />
    
    </Grid>
    

    希望这会有所帮助。享受编码:)

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2011-02-03
      • 2015-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      相关资源
      最近更新 更多