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

时间:2018-02-21 16:53:30

标签: xaml xamarin xamarin.forms uwp

我有一个Xamarin Forms项目(v2.5),我的Xaml文件中有一个文本Entry控件。我需要条目高于默认值,所以我指定一个60的HeightRequest,它工作正常,除了文本本身与Entry控件的顶部对齐。

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

看起来像:

enter image description here

我添加了自定义渲染器:

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;
        }

    }

}

但这不起作用。 Xaml中的HeightRequest似乎不再起作用,所以我在自定义渲染器中添加了高度。但是文本对齐仍然位于顶部。

有谁能告诉我如何让文本垂直居中?

4 个答案:

答案 0 :(得分:0)

我认为这不需要自定义渲染器,只需要居中和扩展。

VerticalOptions = "LayoutOptions.CenterAndExpand"

答案 1 :(得分:0)

UWP应用中Entry的对应原生控制为TextBox,有关详细信息,请参阅Renderer Base Classes and Native ControlsVerticalAlignment属性表示将当前control设置为父控件中的垂直居中,而不是内部文本。只有TextAlignment等属性才会对文字产生影响。由于UWP应用中的Textbox没有属性VerticalTextAlignment,因此您无法直接将文本设置为垂直中心。但您可以将TextBox的样式模板更改为解决方法。

Textbox创建新样式,并将VerticalAlignment属性设置为ContentPresenterScrollViewerControlTemplate控件的中心。然后在自定义渲染中应用样式。

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"];
   }
}

答案 2 :(得分:0)

我知道已经很晚了,但是下面的代码适用于Android以使Entry中的文本居中,它也适用于UWP:

this.Control.Gravity = GravityFlags.CenterHorizontal;

this.Control.Gravity = GravityFlags.Center;

如果有帮助,请告诉我

答案 3 :(得分:-2)

//试试这个:

VerticalOptions =“CenterAndExpand”

如果这不起作用,请转到自定义渲染器