简体   繁体   English

UWP XAML:使用2个元素(一个Viewbox)填充屏幕

[英]UWP XAML: Filling the screen with 2 elements, one Viewbox

I have a grid with two elements, a scaling Viewbox and a Textblock. 我有一个包含两个元素的网格,一个缩放的Viewbox和一个Textblock。 I want to have the Viewbox take only the space it needs, but also only the space it can get. 我希望Viewbox仅占用其所需的空间,而且仅占用其可以获得的空间。

Images explain it much better, the desired image first: 图片会更好地解释它,首先需要所需的图片:
我希望它的工作方式
However, when I resize my application to be wider, the Viewbox starts to overtake the Textblock below it: 但是,当我将应用程序调整为更宽时,Viewbox开始超过其下面的Textblock:
Viewbox超越了Textblock

Here's a dumbed down version of my XAML: 这是我的XAML的简化版本:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" x:Name="MainGrid" Margin="0" UseLayoutRounding="False">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="0" />
    </Grid.ColumnDefinitions>

    <Viewbox Stretch="Uniform" VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Row="0" x:Name="Zulrah">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Rectangle Grid.Column="0" Fill="Blue" Width="150" Height="250" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
            <Rectangle Grid.Column="1" Fill="Red" Width="150" Height="250" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
        </Grid>
    </Viewbox>
    <TextBlock Grid.Row="1" x:Name="TextOutput" MinHeight="100" MinWidth="100">
        Hello world!
        <LineBreak />
        Life's good
    </TextBlock>
</Grid>

You can more or less ignore the second column (with Width="0"), it's used for when the application becomes wide-screen (or landscape). 您可以或多或少地忽略第二列(宽度=“ 0”),该列用于应用程序变为宽屏(或横向)时。 It has the same issue: 它具有相同的问题:
进入“人像”模式时也会发生相同的问题

In short: I want the TextBlock to obey it's MinHeight="100", while still maximizing the space the Viewbox uses. 简而言之:我希望TextBlock服从它的MinHeight =“ 100”,同时仍然最大化Viewbox使用的空间。

PS: Please note that some settings make the Viewbox scale to a larger size than actually fits on the screen, this is not desireable! PS:请注意,有些设置会使Viewbox缩放到比屏幕上实际显示的尺寸更大的尺寸,这是不希望的!

Edit: Remarkably, setting a MinHeight="100" on the second row has no effect... 编辑:值得注意的是,在第二行上设置MinHeight =“ 100”无效...

Since you are using ThemeResource in your code, I think you are developing an UWP app as there is no ThemeResource in WPF. 由于您在代码中使用ThemeResource ,因此我认为您正在开发UWP应用,因为WPF中没有ThemeResource If so, please remove WPF in your title and tags as they are two different frameworks. 如果是这样,请删除标题和标签中的WPF,因为它们是两个不同的框架。 Mixed use of UWP and WPF may cause confusion. UWP和WPF的混合使用可能会引起混淆。

For UWP apps, in Grid , while setting row's height to Auto , the row will size to fit its content. 对于UWP应用,在Grid ,将行的高度设置为Auto时 ,行的大小将适合其内容。 After the Auto rows are calculated, the row which height is set to * will get part of the remaining height. 计算自动行后,将高度设置为*的行将获得剩余高度的一部分。

According to your description, you want the TextBlock to obey it's MinHeight and the Viewbox gets part of the remaining height. 根据您的描述,您希望TextBlock服从它的MinHeight ,并且Viewbox会获得剩余高度的一部分。 So you can change the RowDefinitions like following: 因此,您可以像下面那样更改RowDefinitions

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

And to make the Viewbox fill the remaining area, we can set VerticalAlignment and HorizontalAlignment to Stretch . 为了使Viewbox填充剩余的区域,我们可以将VerticalAlignmentHorizontalAlignment设置为Stretch Besides this, you may also need to set Stretch property to Fill to make the content in Viewbox resize to fill the destination dimensions. 除此之外,您可能还需要将Stretch属性设置为Fill以使Viewbox的内容调整大小以填充目标尺寸。

The complete XAML code may like following: 完整的XAML代码可能如下所示:

<Grid x:Name="MainGrid"
      Margin="0"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
      UseLayoutRounding="False">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="0" />
    </Grid.ColumnDefinitions>

    <Viewbox x:Name="Zulrah"
             Grid.Row="0"
             AllowDrop="True"
             Stretch="Fill">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Rectangle Grid.Column="0"
                       Width="150"
                       Height="250"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Stretch"
                       Fill="Blue" />
            <Rectangle Grid.Column="1"
                       Width="150"
                       Height="250"
                       HorizontalAlignment="Stretch"
                       VerticalAlignment="Stretch"
                       Fill="Red" />
        </Grid>
    </Viewbox>
    <TextBlock x:Name="TextOutput"
               Grid.Row="1"
               MinWidth="100"
               MinHeight="100">
        Hello world!
        <LineBreak />
        Life's good
    </TextBlock>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM