如何在 MaterialEditorRenderer 编辑器中启用复制/粘贴/全选菜单

问题描述

我正在使用 Xamarin Forms 来呈现允许多行文本的编辑器。但是,当我在编辑器框已经有超过 1 行文本的情况下长按时,菜单不会显示以选择/剪切/复制文本。如何启用此菜单?如果它是一行文本,它就可以工作。

我已经将它设置为 true,所以它应该可以工作但不工作。 Control.EditText.SetTextIsSelectable

这是我在 Android 项目中的代码

    [assembly: ExportRenderer(typeof(CustomEditor),typeof(CustomEditorRenderer))]
    
    namespace MyProject.Droid.MRenderers
    {
        public class CustomEditorRenderer : MaterialEditorRenderer
        {
            public CustomEditorRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
            {
                base.OnElementChanged(e);
                Control?.SetBackgroundColor(Android.Graphics.Color.Transparent);
    
                if (Control != null)
                {
                        //This is the HeightRequest 
                        var ElementHeightRequest = Element.HeightRequest;
                        //Convert it to Pixels
                        var EditTextHeight= ConvertToPixels(ElementHeightRequest);
                        //Set the Control's EditText height 
                        Control.EditText.SetHeight(200);
                        Control.EditText.SetTextIsSelectable(true);                
                    Control.EditText.Background = null;
                    Control.EditText.SetBackgroundColor(Android.Graphics.Color.Transparent);
                }
            }

解决方法

您可以在渲染器中设置 AutoSize 属性而不是 SetHeight

自定义渲染器:

[assembly: ExportRenderer(typeof(CustomEditor),typeof(CustomEditorRenderer))]
namespace Test.Droid
{
public class CustomEditorRenderer : EditorRenderer
{
    public CustomEditorRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
            return;

        Control.Background = null;
        Control.SetPadding(0,0);
        Control.ShowSoftInputOnFocus = false;
        Control.SetTextIsSelectable(true);
       
    }

}
}

XML:

        <local:CustomEditor AutoSize="TextChanges" Text="I am using Xamarin Forms to render an Editor that allows for multi-line text. However,when I long press in the Editor box when it already has more than 1 line of text,the menu is not showing up to select/cut/copy the text. How do I enable this menu? It works if it's a single line of text. I have already set this to true,so it should work but doesn't work."></local:CustomEditor>
    

您可以检查输出。 https://imgur.com/KcNtFH4

更新:

我没有对渲染器做任何更改。它可以在 MaterialEditorRenderer 编辑器中显示复制/粘贴/选择菜单。

[assembly: ExportRenderer(typeof(CustomMaterialEditor),typeof(CustomMaterialEditorRenderer))]
namespace Test3.Droid
{
public class CustomMaterialEditorRenderer : MaterialEditorRenderer
{
    public CustomMaterialEditorRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
            return;

        Control.Background = null;
        Control.SetPadding(0,0);
        //Control.DispatchSetSelected(true);               
        //Control.ShowSoftInputOnFocus = false;
        //Control.SetTextIsSelectable(true);

    }      
}
 }

XML:

   <local:CustomMaterialEditor
            AutoSize="TextChanges"
            BackgroundColor="LightGray"
            Text="I am using Xamarin Forms to render an Editor that allows for multi-line text. However,so it should work but doesn't work." />

enter image description here