在UWP中居中RelativePanels C#
问题描述:
我正在尝试将HorizontalAlignment和VerticalAlignment设置为居中,以便RelativePanel将在UWP应用程序中居中。在RelativePanel里面有儿童项目,并且儿童应该在里面居中RelativePanel。它应该看起来像这样:正如你所看到的,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>
下面是调试时的样子:
答
将RelativePanel.AlignHorizontalCenterWithPanel="True"
添加到您的TextBlock
和TextBox
。
<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"
。
谢谢!它完全奏效。你能告诉我在我以前的代码中我做错了什么吗? –
是的,你基本上需要在你的元素上使用'RelativePanel.xxx'来相应地定位它们。看看这个[链接](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.relativepanel)。 –