maxipunchik
maxipunchik

Reputation: 17

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

Upvotes: 2

Views: 1596

Answers (3)

maxipunchik
maxipunchik

Reputation: 17

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

Upvotes: 0

Nikhileshwar
Nikhileshwar

Reputation: 1684

There is no Template API or any other API (Button API docs) to add a custom View to button in Xamarin as Iavn pointed out. And neither does TextBlock exists in Xamarin.

Since you are adding only TextBlock consider the Text API of Button.

<Button
    Text="The Text you want"
    FontSize="Small"/>

For using a custom View as a button you can use TapGestureRecogniser. TapGesture can be added to any View in Xamarin.

    <Label
        Text="Some text">
        <Label.GestureRecognizers>
            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
        </Label.GestureRecognizers>
    </Label>

Upvotes: 0

Ivan I
Ivan I

Reputation: 9990

You are using WPF/UWP XAML. It is not supported in Xamarin. It is out of scope to answer here on what are the differences between those and you should learn it.

But to answer your question (and that is not the only problem in the code above), there is no TextBlock in Xamarin, but Label. But just this won't help you work as complete logic used above is flawed.

Upvotes: 2

Related Questions