在UWP中居中RelativePanels C#

问题描述:

我正在尝试将Horizo​​ntalAlignment和VerticalAlignment设置为居中,以便RelativePanel将在UWP应用程序中居中。在RelativePanel里面有儿童项目,并且儿童应该在里面居中RelativePanel。它应该看起来像这样:enter image description here正如你所看到的,RelativePanel在中间,并且孩子们在的集中的顶部。我想这XAML代码:在UWP中居中RelativePanels C#

<RelativePanel x:Name="relativePanel1" HorizontalAlignment="Center" VerticalAlignment="Center"> 
    <TextBlock x:Name="textBlock1" Text="Yo, waddup? Enter your GUID to continue" IsTextSelectionEnabled="True" FontFamily="Chiller" FontSize="72" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
    <TextBox x:Name="textBox1" AcceptsReturn="True" HorizontalAlignment="Center" InputScope="Text" PlaceholderText="{}{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" RelativePanel.Below="textBlock1" VerticalAlignment="Center" /> 
</RelativePanel> 

下面是调试时的样子:

enter image description here我是不是做错了这里?我使用的是一个RelativePanel而不是StackPanel,因为我需要两个孩子在彼此之上。

RelativePanel.AlignHorizontalCenterWithPanel="True"添加到您的TextBlockTextBox

<RelativePanel x:Name="relativePanel1" 
       HorizontalAlignment="Center" 
       VerticalAlignment="Center"> 
    <TextBlock x:Name="textBlock1" 
       RelativePanel.AlignHorizontalCenterWithPanel="True" 
       Text="Yo, waddup? Enter your GUID to continue" 
       IsTextSelectionEnabled="True" 
       FontFamily="Chiller" 
       FontSize="72" /> 
    <TextBox x:Name="textBox1" 
      RelativePanel.AlignHorizontalCenterWithPanel="True" 
      AcceptsReturn="True" 
      InputScope="Text" 
      PlaceholderText="{}{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" 
      RelativePanel.Below="textBlock1" /> 
</RelativePanel> 

虽然可能还有更多。你的RelativePanel显然看起来顶部对齐给我。这意味着它的父母面板不是延伸。另外,如果居中完成,则不必在此处使用RelativePanel。您可以用StackPanel替换它,并在其所有子级上手动设置HorizontalAlignment="Center"

+1

谢谢!它完全奏效。你能告诉我在我以前的代码中我做错了什么吗? –

+0

是的,你基本上需要在你的元素上使用'RelativePanel.xxx'来相应地定位它们。看看这个[链接](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.relativepanel)。 –