Xaml 如何在UWP上垂直居中放置Xamarin表单条目中的文本?

Xaml 如何在UWP上垂直居中放置Xamarin表单条目中的文本?,xaml,xamarin,xamarin.forms,uwp,Xaml,Xamarin,Xamarin.forms,Uwp,我有一个XamarinForms项目(v2.5),其中在我的Xaml文件中有一个文本输入控件。我需要条目比默认值高,所以我指定HeightRequest为60,这很好,除了文本本身与条目控件的顶部对齐之外 <Entry Text="{Binding CustomerNameText}" HeightRequest="60" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" IsEnabled="{Bindin

我有一个XamarinForms项目(v2.5),其中在我的Xaml文件中有一个文本输入控件。我需要条目比默认值高,所以我指定HeightRequest为60,这很好,除了文本本身与条目控件的顶部对齐之外

<Entry Text="{Binding CustomerNameText}" HeightRequest="60" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" IsEnabled="{Binding CustomerNameEntryEnabled}" Focused="Entry_Focused" Unfocused="Entry_Unfocused" />

这看起来像:

我添加了一个自定义渲染器:

public class CustomEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if(this.Control != null)
        {
            this.Control.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Center;
            this.Control.Height = 60;
        }

    }

}
公共类CustomEntryRenderer:EntryRenderer { 受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e) { 基础。一个要素发生变化(e); if(this.Control!=null) { this.Control.VerticalAlignment=Windows.UI.Xaml.VerticalAlignment.Center; 此.Control.Height=60; } } } 但这不起作用。Xaml中的HeightRequest似乎不再有效,因此我在自定义渲染器中添加了高度。但文本对齐仍保持在顶部


有人能告诉我如何使文本垂直居中吗?

我认为不需要自定义渲染器,只需居中并展开即可

VerticalOptions = "LayoutOptions.CenterAndExpand"
//试试这个:

VerticalOptions=“CenterAndExpand”


如果这不适用于自定义渲染器

在UWP应用程序中,
条目
的相应本机控件是
文本框
,请参阅以获取更多详细信息。该属性意味着将当前
控件设置为父控件中的垂直中心,而不是内部的文本。只有类似的属性才会对文本生效。由于UWP应用程序中的
Textbox
没有属性
VerticalTextAlignment
,因此无法直接将文本设置为垂直中心。但是您可以更改
TextBox
的样式模板作为解决方法

文本框
创建新样式,并将
垂直对齐
属性设置为
内容演示器
控制模板
内的滚动查看器
控件的中心。然后在自定义渲染中应用样式

App.xaml中的样式

<Style x:Key="TextBoxStyle1" TargetType="TextBox">
...
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid> 
                  ...
                    <Border x:Name="BorderElement" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" Grid.RowSpan="1" Grid.Row="1"/>
                    <ContentPresenter  x:Name="HeaderContentPresenter" VerticalAlignment="Center"  ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.ColumnSpan="2" FontWeight="Normal" Foreground="{ThemeResource TextControlHeaderForeground}" Margin="0,0,0,8" Grid.Row="0" TextWrapping="{TemplateBinding TextWrapping}" Visibility="Collapsed" x:DeferLoadStrategy="Lazy"/>
                    <ScrollViewer x:Name="ContentElement" VerticalAlignment="Center" AutomationProperties.AccessibilityView="Raw" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsTabStop="False" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" ZoomMode="Disabled"/>
                    <TextBlock x:Name="PlaceholderTextContentPresenter" Grid.ColumnSpan="2" Foreground="{Binding PlaceholderForeground, RelativeSource={RelativeSource Mode=TemplatedParent}, TargetNullValue={ThemeResource TextControlPlaceholderForeground}}" IsHitTestVisible="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1" Text="{TemplateBinding PlaceholderText}" TextWrapping="{TemplateBinding TextWrapping}" TextAlignment="{TemplateBinding TextAlignment}"/>
                    <Button x:Name="DeleteButton" AutomationProperties.AccessibilityView="Raw" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="1" FontSize="{TemplateBinding FontSize}" IsTabStop="False" MinWidth="34" Margin="{ThemeResource HelperButtonThemePadding}" Grid.Row="1" Style="{StaticResource DeleteButtonStyle}" VerticalAlignment="Stretch" Visibility="Collapsed"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

...
...
自定义渲染:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
   base.OnElementChanged(e);

   if (this.Control != null)
   { 
       this.Control.Height = 60; 
       Control.Style = (Windows.UI.Xaml.Style)App.Current.Resources["TextBoxStyle1"];
   }
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(this.Control!=null)
{ 
此.Control.Height=60;
Control.Style=(Windows.UI.Xaml.Style)App.Current.Resources[“TextBoxStyle1”];
}
}

我知道已经很晚了,但下面的代码适用于Android在条目中居中显示文本,这也适用于UWP:

this.Control.Gravity = GravityFlags.CenterHorizontal;

this.Control.Gravity = GravityFlags.Center;

让我知道,如果有帮助的话

Hi@aritchie你有解决办法吗?