在编辑器 Xamarin Forms 中设置 CursorPosition [英] Set CursorPosition in Editor Xamarin Forms

查看:49
本文介绍了在编辑器 Xamarin Forms 中设置 CursorPosition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在Editor 中设置CursorPosition.我们可以在Entry中设置CursorPostion,但是如何在Editor中设置呢?我知道我们可以使用 Xamarin Forms 中的自定义渲染器来实现,但如何实现它?

How can we set CursorPosition in Editor. We can set CursorPostion in Entry, but how to set it in Editor? I know we can do it using Custom Renderer in Xamarin Forms, but how to implement it?

推荐答案

我们不能在Editor中设置CursorPosition,因为我们在Entry中设置了.我们需要使用自定义渲染器.

We cannot set CursorPosition in Editor as we set it in Entry. We need to use Custom Renderer.

使用自定义渲染器可以在 Xamarin Forms 中实现大部分本机功能和实现.

Using Custom Renderer makes most of the native functions and implementations possible in Xamarin Forms.

在 Xamarin Forms 的 Editor 中设置光标位置.对于像我这样的初学者来说,这个答案可能有点长,所以请耐心等待.

To set Cursor Position in Editor in Xamarin Forms. This answer may be a bit lenghthier for beginners like me so please bear with me.

在您的共享项目中添加一个类 EditorExtended.cs

public class EditorExtended : Editor
{
}

在您的 XAML 页面中添加命名空间以供参考

In your XAML Page add namespace for reference

<xmlns:Local="clr-namespace:ApplicationName.FolderNameThatContainsEditorExtended class">

<!-- If EditorExtended.cs is in "Controls" Folder-->
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
             xmlns:Local="clr-namespace:MyApplication.Controls">
            
             ...

    <StackLayout HorizontalOptions="Center">             
        <Local:EditorExtended x:Name="CustomEditor"></Local:EditorExtended>
    </StackLayout>

  • 现在在您的 Android 项目中添加自定义渲染器.创建一个文件夹并添加EditorRendererExtended.cs
    • Now in your Android Project add Custom Renderer. Make a folder and add EditorRendererExtended.cs
    • [assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
      namespace MyApplication.Droid.PlatformSpecific.ExtendedControls
      {
          public class EditorRendererExtended : EditorRenderer
          {
              public EditorRendererExtended(Context context) : base(context)
              {
              }
      
              protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
              {
                  base.OnElementPropertyChanged(sender, e);
                  if (Control != null)
                  {
                      Control.RequestFocus();                
                      Control.SetSelection(Control.Text.Length);                
                  }
              }       
          }
      }
      

      • 类似在 UWP 项目 中创建一个文件夹并在特定于平台的代码中添加自定义渲染器.
        • Similarly in UWP Project Make a folder and add Custom Renderer in Platform Specific code.
        • [assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
          namespace MyApplication.UWP.PlatformSpecific.ExtendedControls
          {
              public class EditorRendererExtended: EditorRenderer
              {
                  public EditorRendererExtended()
                  {
                  }
          
                  protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
                  {
                      base.OnElementPropertyChanged(sender, e);
                      if (Control != null)
                      {
                          Control.Focus(Windows.UI.Xaml.FocusState.Pointer);
                          Control.SelectionStart = Control.Text.Length;
                      }
                  }
              }
          }
          

          • 我还没有在 iOS 中测试过,但方法是类似的.只需在 iOS 平台特定文件夹中添加 EditorRendererExtended.cs 类.此代码未经测试,如果您知道解决方案,您可以自由编辑答案.这是我已实现但未测试的代码.
            • I haven't tested in iOS, but the method will be similar. Just add EditorRendererExtended.cs class in Platform Specific folder in iOS. This code is not tested, you are free to edit the answer if you know the solution. Here is the code I have implemented, but not tested.
            • [assembly:ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
              namespace MyApplication.iOS.PlatformSpecific.ExtendedControls
              {
                  public class EditorRendererExtended : EditorRenderer
                  {
                      public EditorRendererExtended()
                      {
                      }
              
                      protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
                      {
                          base.OnElementPropertyChanged(sender, e);
                          if (Control != null)
                          {
                              // just change this statement to the one that works.
                              Control.SelectedTextRange = Control.GetTextRange(fromPosition: Control.BeginningOfDocument, toPosition: Control.BeginningOfDocument);
                          }
                      }
                  }
              }
              

              不要忘记在您要定位的所有平台特定代码中包含以下语句,否则将无法运行

              Do not forget to include the below statement in all the Platform Specific code that you want to target, otherwise it won't work

              [assembly: ExportRenderer(typeof(EditorExtended), typeof(EditorRendererExtended))]
              

              EditorRendererExtended 因平台而异,您可以将其名称更改为 EditorRendererExtendedAndroidEditorRendererExtendedUWP 以便更好地理解.我只是将它们命名为相似的,因为我发现不需要以不同的方式命名它们,并且会不必要地冗长.

              EditorRendererExtended is different for each platform, you can change its name like EditorRendererExtendedAndroid or EditorRendererExtendedUWP for better understanding. I have just named them similar as I find naming them differently is not needed and makes it unnecessarily lengthy.

              这篇关于在编辑器 Xamarin Forms 中设置 CursorPosition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆