Xaml 表单:如何使多行标签居中?

Xaml 表单:如何使多行标签居中?,xaml,xamarin,xamarin.forms,grid,label,Xaml,Xamarin,Xamarin.forms,Grid,Label,我尝试垂直居中一个包含可以显示在1行或2行上的文本 目前,我无法获得预期的渲染 我可以使用自动延伸的多行标签,但它是顶部对齐且不居中的: 这是随附的XAML: <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> <RowDefinit

我尝试垂直居中一个包含可以显示在1行或2行上的文本

目前,我无法获得预期的渲染

我可以使用自动延伸的多行标签,但它是顶部对齐且不居中的:

这是随附的XAML:

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

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    ...
/>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="0" />
</Grid.RowDefinitions>

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    VerticalOptions="FillAndExpand"
    ...
/>

我也可以将标签居中,但在这种情况下,它不会自动延伸:

这是随附的XAML:

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

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    ...
/>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
    <RowDefinition Height="0" />
</Grid.RowDefinitions>

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    VerticalOptions="FillAndExpand"
    ...
/>


你能解释一下吗?我还尝试在标签周围添加StackLayound,但这没有改变任何事情…

控件居中的第一个选择是使用
VerticalOptions=“CenterAndExpand”
,您是否尝试过使用它而不是
VerticalOptions=“FillAndExpand”
?关于Xamarin.Forms中的布局选项

因此,这是垂直居中的正确方法:

<local:MultiLineLabel
    Grid.Row="0"
    Grid.Column="1"
    BackgroundColor="Orange"
    Text="{ Binding encart_titre }"         
    VerticalTextAlignment="Center"
    LineBreakMode="TailTruncation"
    Lines="2"
    VerticalOptions="CenterAndExpand" <!-- This one here -->
    ...
/>

谢谢,它与
VerticalOptions=“CenterAndExpand”
配合使用,只是有一点规范:标签必须嵌入
StackLayout
中,使用
VerticalOptions=“CenterAndExpand”
horizontalpoptions=“CenterAndExpand”