简体   繁体   English

按钮内的 Xamarin.Forms TextBlock。 自定义文本换行

[英]Xamarin.Forms TextBlock within button. Custom wrapping of a text

Xamarin.Forms XAML. 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:但是 TextBlock 给了我错误:

XLS0414 The type 'TextBlock' was not found. XLS0414 找不到类型“TextBlock”。 Verify that you are not missing an assembly reference and that all referenced assemblies have been built验证您没有缺少程序集引用并且所有引用的程序集都已构建

You are using WPF/UWP XAML.您正在使用 WPF/UWP XAML。 It is not supported in Xamarin. 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.但是要回答您的问题(这不是上面代码中的唯一问题),Xamarin 中没有 TextBlock,而是 Label。 But just this won't help you work as complete logic used above is flawed.但这并不能帮助您工作,因为上面使用的完整逻辑存在缺陷。

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.正如 Iavn 指出的那样,没有Template API 或任何其他 API (按钮 API 文档)可以将自定义视图添加到 Xamarin 中的按钮。 And neither does TextBlock exists in Xamarin. Xamarin 中也不存在TextBlock

Since you are adding only TextBlock consider the Text API of Button .由于您只添加 TextBlock,请考虑ButtonText API。

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

For using a custom View as a button you can use TapGestureRecogniser.要将自定义视图用作按钮,您可以使用 TapGestureRecogniser。 TapGesture can be added to any View in Xamarin. TapGesture 可以添加到 Xamarin 中的任何视图。

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

More Simply then i thought.更简单,然后我想。 Text must be set in cs File not in XAML.文本必须在 cs 文件中设置,而不是在 XAML 中。 Example: Before Контрагент - ы After Контра - генты.示例:在 Контрагент - ы 之前,在 Контра - генты 之后。 There is a code:有一个代码:

` `

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

` `

In XAML i couldn't use \\n operator在 XAML 中我不能使用 \\n 运算符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM