如何使Button扩展以在Xamarin.Forms中显示其Text

时间:2016-09-17 14:43:42

标签: xamarin.forms

我为我的按钮创建了Style,但是当我使用较长的文本时,会截断

这是风格:

<Style x:Key="DefaultButton" TargetType="Button">
        <!--<Setter Property="WidthRequest">
            <Setter.Value>
                <OnIdiom x:TypeArguments="x:Double"
                         Phone="150"
                         Tablet="200" />
            </Setter.Value>
        </Setter>
        <Setter Property="HeightRequest">
            <Setter.Value>
                <OnIdiom x:TypeArguments="x:Double"
                         Phone="70"
                         Tablet="100" />
            </Setter.Value>
        </Setter>-->
        <Setter Property="BackgroundColor" Value="{StaticResource BaseColor}" />
        <Setter Property="TextColor" Value="White" />
        <Setter Property="HorizontalOptions" Value="CenterAndExpand" />
        <Setter Property="VerticalOptions" Value="FillAndExpand" />
        <Setter Property="Margin" Value="5" />
        <Setter Property="FontSize">
            <Setter.Value>
                <OnIdiom x:TypeArguments="x:Double"
                         Phone="20"
                         Tablet="25" />
            </Setter.Value>
        </Setter>
    </Style>

按钮:

<Button Grid.Row="1"
            Grid.Column="1"
            Style="{DynamicResource DefaultButton}"
            Text="Basic button with long text" />

这是具有较长文本的按钮的外观:

enter image description here

我可以为按钮设置一个非常大的HeightRequest,但这样做非常糟糕。

我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以使用LineBreakMode属性。这是帮助您选择最合适模式的文档。

https://developer.xamarin.com/api/type/Xamarin.Forms.LineBreakMode/

答案 1 :(得分:0)

在这种情况下,我会经常使用带有GridBoxView的{​​{1}}来构成按钮,然后在Label上放置GestureRecognizer }。将所有这些添加到自定义控件中,以便在需要时轻松重用。

我在内存中执行了Grid,因此可能需要一些修复:

GestureRecognizer