【问题标题】:Unable to select text in Xamarin.Forms Editor control or custom Borderless Editor using renderer无法在 Xamarin.Forms 编辑器控件或使用渲染器的自定义无边框编辑器中选择文本
【发布时间】:2020-07-20 12:57:31
【问题描述】:

我已经使用自定义 EditorRenderer 在我的 Xamarin.Forms 应用程序中实现了无边框编辑器。但是我面临一个问题,即在这两种情况下,无论是在表单编辑器控件中还是在渲染的本机编辑器控件中,编辑器文本都是不可选择的。我的应用程序具有让用户在输入时在编辑器中复制粘贴文本的功能,就像在任何其他文本编辑应用程序中一样。这是大多数应用程序的基本功能,默认情况下存在。但它在我的应用程序中不起作用。我尝试通过

启用它

Control.SetTextIsSelectable(true);

但它仍然无法正常工作。我也尝试过其他的东西,比如:

Control.CustomSelectionActionModeCallback = 新 CustomSelectionActionModeCallback(); Control.CustomInsertionActionModeCallback = new CustomInsertionActionModeCallback();

但是根本没有任何效果,甚至一个单词都没有选择文本。有人对这个问题有任何想法吗?如何使文本可选择并允许自定义编辑器中的默认复制粘贴功能?

这是我在 Xaml 中的代码:

 <renderer:BorderlessEditor
                                        Grid.Row="1"
                                        x:Name="UserTextEditorAndroid"
                                        BackgroundColor="{StaticResource WhiteColor}"
                                        HeightRequest="350"
                                        Margin="20,2"
                                        MaxLength="1024"
                                        IsReadOnly="{Binding Source={x:Reference LongTextTemplate}, Path=Editable, Converter={StaticResource InverseBool}}" />

而自定义渲染代码是:

public class BorderlessEditorRenderer : EditorRenderer
{
    public BorderlessEditorRenderer()
    {
    }

    public static void Init() { }
    protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
    {
        base.OnElementChanged(e);
        if (e.OldElement == null)
        {
            Control.Background = null;

            var layoutParams = new MarginLayoutParams(Control.LayoutParameters);
            layoutParams.SetMargins(0, 0, 0, 0);
            LayoutParameters = layoutParams;
            Control.LayoutParameters = layoutParams;
            Control.SetPadding(0, 0, 0, 0);
            SetPadding(0, 0, 0, 0);
            Control.SetTextIsSelectable(true);
            Control.VerticalScrollBarEnabled = false;
        }
    }
}

即使我在 Xaml 中使用 Xamarin.Forms 自己的编辑器而不是自定义渲染器,它也根本不起作用。文本仍然无法选择。

【问题讨论】:

  • 我用自定义渲染器定义了编辑器。但我无法产生问题。您最好发布完整的代码或分享一个示例,以便我可以测试它。
  • 我的 Xaml 代码:
  • 在您的问题中发布 xaml 和自定义渲染器的完整代码。
  • 好的,我会更新我的帖子
  • 当我长按文本时它工作正常。请注意,如果IsReadOnly 为真,我们无法将值粘贴到编辑器。

标签: xamarin.forms xamarin.android custom-controls text-editor textselection


【解决方案1】:

我遇到了同样的问题 - 但我没有使用 Telerik。我在 Win 10 上运行 VS 2019。这很容易重现 ...

创建一个新的“移动应用程序(Xamarin 表单)”并将编辑器添加到 MainPage.xaml 文件。我还将编辑器放在 ScrollView 中以进行踢球。下面的代码只显示了编辑器中放置的几个文本单词,但我实际上使用了 Microsoft 文档中的一个长段落。我在下面省略了它,以简化代码演示......并避免混乱!

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="Editor_Test.MainPage">

    <StackLayout>
        <!-- Place new controls here -->
        <ScrollView>
            <Editor Text="Visual Studio makes it easier ..."
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand" />
        </ScrollView>
    </StackLayout>

</ContentPage>

我的行为正常:

  • 因为有了 ScrollView,我可以用手指(鼠标)拖动滚动文本
  • 长按(单击)选择按下的单词
  • 双击(单击)可选择所点击的单词

如果我在 App.xaml.cs 中更改 App ctor 中的 1 行代码以使用 NavigationPage 实例化 MainPage,则会得到不同的结果,如下所示:

public App()
{
    InitializeComponent();

    //MainPage = new MainPage();
    MainPage = new NavigationPage(new MainPage());
}

现在行为发生了变化:

  • 长按(点击)无效(无选择)
  • 双击(单击)无效(无选择)

事实上,我根本看不到在编辑器中选择文本。我不知道为什么会发生这种情况……但确实如此。这让我有点生气,直到我意识到是什么原因造成的。

我正在使用:

  • Xamarin 表单 4.5.0.495
  • Xamarin Essentials 1.3.1
  • 为 Android 9 (Pie) 构建

我是 Android/Xamarin 的新手......所以我的问题是,如何最好地实现我自己的简单页面导航 - 即,不使用 Xamarin 的 NavigationPage?我有一个简单的 3 页应用程序,我想在这方面取得一些进展!

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    相关资源
    最近更新 更多