在Xamarin.Forms中更新后编辑器中的中心文本

时间:2017-09-16 05:26:49

标签: c# xamarin xamarin.forms

我试图在中心显示对齐的文字。问题是,一旦我更新文本,它就会被切断。 如果我使用" Fill"作为Horizo​​ntalOptions,文本不会被截断,但文本不在中心。

示例基于Forms:

中的编辑器示例
public class EditorPageCode : ContentPage
{
    Editor styledEditor = new Editor();
    Editor centerText = new Editor();
    Editor customEditor = new Editor();

    public EditorPageCode()
    {
        var layout = new StackLayout { Padding = new Thickness(5, 10) };
        this.Title = "Editor Demo - Code";
        layout.Children.Add(new Label { Text = "This page demonstrates the Editor View. The Editor is used for collecting text that is expected to take more than one line." });
        styledEditor = new Editor
        {
            Text = "Xamarin Blue",
            BackgroundColor = Color.FromHex("#2c3e50"),
            HeightRequest = 100,
            HorizontalOptions = LayoutOptions.Center
        };
        customEditor = new Editor { Text = "Default starting text", HorizontalOptions = LayoutOptions.Center };
        customEditor.Focused += StyledEntry_Focused;
        layout.Children.Add(customEditor);
        centerText = new Editor
        {
            IsEnabled = false,
            Text = "This is a disabled editor",
            HorizontalOptions = LayoutOptions.Fill

        };
        layout.Children.Add(centerText);
        this.Content = layout;
    }
    void StyledEntry_Focused(object sender, FocusEventArgs e)
    {
        var text = centerText.Text + "new ";
        centerText.Text = text;
        centerText.HorizontalOptions = LayoutOptions.Center;
    }

}

1 个答案:

答案 0 :(得分:0)

我实际上调查了消息来源。一个对我有用的解决方案,因为我不需要在这个特定的字段上进行编辑就是使用标签。

PaneContentGrid

Label具有Horizo​​ntalTextAlignment,它在更新后将文本保持在中心位置。 编辑器控件没有。