如何在Xamarin.Forms中创建多行文本框

24

如何在Xamarin.Forms中创建多行文本框?

经过一些研究,我发现可以使用XAML页面中的<Editor>标签来完成,还可以自定义字体大小和样式,但这会创建一个居中对齐的文本框。然而,我想要从左上角开始插入光标和文本。


你应该放置一些代码,例如编辑器和其父级。一个没有任何属性但有高度的编辑器,默认是多行且不居中的。 - Benoit Jadinon
在传统的Xamarin中,您只需在属性中选择inputType为textMultiLine即可实现此功能。 - Bengi Besçeli
3个回答

21

编辑器用于收集需要占据多行的文本。

例如:

<?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="TextSample.EditorPage"
Title="Editor">
    <ContentPage.Content>
        <StackLayout>
            <Editor Text="Editor is used for collecting text that is expected to take more than one line." BackgroundColor="#2c3e50" HeightRequest="100" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

7
您可以像下面的代码一样使用一个Editor,并指定最大字符数:
<Editor Keyboard="Plain" AutoSize="TextChanges" MaxLength="180" />

注意:使用 AutoSize="TextChanges" 选项,编辑器会在用户输入时自动调整大小以适应输入内容。默认情况下,自动调整大小是关闭的。

6
你提到的 Editor,在页面示例中如下所示,无法创建居中文本。
  <StackLayout Padding="5,10">
        <Editor>
            <Editor.BackgroundColor>
                <OnPlatform x:TypeArguments="x:Color"
                    iOS="#a4eaff"
                    Android="#2c3e50"
                    WinPhone="#2c3e50" />
            </Editor.BackgroundColor>
        </Editor>
    </StackLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接