.NET MAUI Editor通过代码添加文本时无法自动滚动至新行
MAUI只读Editor自动滚动到底部问题(Windows/iOS平台)
我使用只读Editor展示长时操作的进度,发现Windows和iOS平台在文本行数超出可视区域后,Editor不会自动滚动到底部,但Android平台表现正常。
复现示例
- 手动在第一个Editor(editorOne)中输入文本时,按下回车键会自动滚动;
- 通过顶部按钮添加文本时,则不会自动滚动到底部。
求该Bug的解决方法。
编辑1:已尝试设置
CursorPosition = editorOne.Text.Length-1,此操作可移动光标但无法使Editor滚动。
编辑2:将以下代码添加到TextChanged事件中可在Windows平台生效,但在iOS平台会唤起键盘:
editorOne.CursorPosition = editorOne.Text.Length - 1; editorOne.Focus();仍在寻找iOS平台的解决方案...
XAML代码
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MauiEditorScrollTest.MainPage"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Label Text="Click button to add text. Once editor is full, does editor scroll so new text is visible?"/> <Button Grid.Row="1" Text="Add Line of Text" Clicked="Button_AddText_Clicked"/> <Editor Grid.Row="2" x:Name="editorOne"/> <Editor Grid.Row="3" x:Name="editorTwo" IsReadOnly="True"/> <Button Grid.Row="4" Text="Erase All" Clicked="Button_Erase_Clicked"/> </Grid> </ContentPage>
后台代码
namespace MauiEditorScrollTest { public partial class MainPage : ContentPage { int LineNumber = 1; public MainPage() { InitializeComponent(); } private void Button_AddText_Clicked(System.Object sender, System.EventArgs e) { editorOne.Text += "Added Line " + LineNumber + Environment.NewLine; editorTwo.Text += "Added Line " + LineNumber + Environment.NewLine; LineNumber++; } private void Button_Erase_Clicked(System.Object sender, System.EventArgs e) { editorOne.Text = ""; editorTwo.Text = ""; LineNumber = 1; } } }
内容的提问来源于stack exchange,提问作者ejwipp
相关产品推荐
相关产品推荐

