Search code examples
xamlxamarinxamarin.formstextblock

Xamarin.Forms TextBlock within button. Custom wrapping of a text


Xamarin.Forms XAML. The text on the button is moved to a new line incorrectly. I need to make it so that I can configure the wrapping. Trying to do like this:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="SaleAgent.MainPage"
Title = "Торговый агент">
    <ContentPage.Resources>

        <Style x:Key="buttonStyle" TargetType="{x:Type Button}">
            <Setter Property="FontSize" Value="Small" />

            <Setter Property="Template">
                    <Setter.Value>
                    <TextBlock></TextBlock> 
                    </Setter.Value>
                </Setter>

        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>
        <Grid x:Name="mainGrid" RowSpacing="1" ColumnSpacing="1">
        <Grid.RowDefinitions>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
        </Grid.ColumnDefinitions>
            <Button Grid.Row="1" Grid.Column="1" Grid.RowSpan="3" Style="{StaticResource buttonStyle}"/>  
        </Grid>
    </ContentPage.Content>
</ContentPage>

But TextBlock give me mistake:

XLS0414 The type 'TextBlock' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built


Solution

  • More Simply then i thought. Text must be set in cs File not in XAML. Example: Before Контрагент - ы After Контра - генты. There is a code:

    `

    public MainPage()
            {
                InitializeComponent();
                ButtonContragent.Text = "Контра\nгенты";
            }
    

    `

    In XAML i couldn't use \n operator