在Maui iOS上如何更改编辑器光标的颜色?

huangapple go评论74阅读模式
英文:

How to change the color of Editor cursor on Maui iOS?

问题

我如何在Maui iOS应用的Editor控件中更改光标的颜色?

在Maui iOS上如何更改编辑器光标的颜色?

英文:

How can I change the color of the cursor in the Editor control on a Maui iOS app?

在Maui iOS上如何更改编辑器光标的颜色?

答案1

得分: 4

虽然渲染器(来自其他回答)仍然可用,但您不应再使用它。这仅存在于与Xamarin.Forms的兼容性原因。

您想要做的是创建并修改映射器。关于如何执行此操作的官方文档可以在这里找到。

如果您想要对应用程序中的所有Entry控件执行此操作,可以执行以下操作:

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CursorColor", (handler, view) =>
{
#if IOS
    handler.PlatformView.TintColor = UIKit.UIColor.Green;
#endif
});

您可以在应用程序启动的任何地方放置这个代码,也许放在您的MauiProgram.cs中。

如果您想要对特定的Entry实例执行此操作,您需要创建一个继承并在上面的代码中进行检查。例如,代码可能如下所示:

class MyGreenEntry : Entry
{
}

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CursorColor", (handler, view) =>
{
#if IOS
    if (view is MyGreenEntry)
    {
        handler.PlatformView.TintColor = UIKit.UIColor.Green;
    }
#endif
});

我创建了一个小示例,可以在这里找到:https://github.com/jfversluis/MauiCustomCursorColoriOSSample,还在这篇博客文章中详细介绍了它:https://blog.verslu.is/maui/change-cursor-color-maui-ios/

编辑:糟糕,刚刚注意到这是关于Editor。基本上都适用相同的方式,但确保使用EditorHandler而不是EntryHandler,当然,如果您只想在一种类型上使用它,需要继承自Editor。我在上面的代码库中更新了示例代码,以包括Editor的代码。

英文:

While renderers (from the other answer) still work, you shouldn't be using it anymore. That only exists for compatibility reasons with Xamarin.Forms.

What you want to do is create a modify the mapper. The official docs on how to do that can be found here.

If you want to do this for all Entry controls in your app, you can do this:

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CursorColor", (handler, view) =>
{
#if IOS
    handler.PlatformView.TintColor = UIKit.UIColor.Green;
#endif
});

Place this anywhere you like somewhere in the startup of your app. Maybe in your MauiProgram.cs.

If you want to do this for a specific instance of an Entry you want to create an inheritance and check for that in the above code. For instance, that would look like this:

class MyGreenEntry : Entry
{
}

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("CursorColor", (handler, view) =>
{
#if IOS
    if (view is MyGreenEntry)
    {
        handler.PlatformView.TintColor = UIKit.UIColor.Green;
    }
#endif
});

I've created a little sample here: https://github.com/jfversluis/MauiCustomCursorColoriOSSample and written more about it in a blog post here: https://blog.verslu.is/maui/change-cursor-color-maui-ios/

Edit: oops, just noticed that this is about the Editor. All the same basically applies, but make sure to use the EditorHandler instead of the EntryHandler and of course inherit from the Editor if you want to use it only on one type. I updated the sample code in the repo just above here to also include the code for Editor.

答案2

得分: 1

你可以使用在.NET MAUI中使用自定义渲染器来实现它。

以下是详细的步骤:

  1. 在项目文件夹中创建 MyEditor.cs
public class MyEditor : Editor 
{

}
  1. Platform/iOS 文件夹下创建平台特定的实现文件 MyEditorRenderer.cs
[Obsolete] 
public class MyEditorRenderer : EditorRenderer
{
     protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
     {
          base.OnElementChanged(e);

          if (Control != null)
          {
              Control.TintColor = UIColor.Green;
      }

   }
}
  1. MauiProgram.cs 中注册我们的处理程序,如下所示:
.ConfigureMauiHandlers(handlers =>
{
#if IOS
     handlers.AddHandler(typeof(MyEditor), typeof(MyEditorRenderer));
#endif
});
  1. 最后,创建编辑器控件的实例,然后运行以进行检查。
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUIProjectWithRenderer.MainPage"
xmlns:editor="clr-namespace:CustomRenderer"
>

<VerticalStackLayout>
    <editor:MyEditor /> 
</VerticalStackLayout>

</ContentPage>
英文:

You can use the Using Custom Renderers in .NET MAUI to achieve it.

Below are the detailed steps:

  1. Create the MyEditor.cs in the Project folder:
public class MyEditor : Editor 
{

}

  1. Create the platform-specific implementation file MyEditorRenderer.cs under Platform/iOS:
[Obsolete] 
public class MyEditorRenderer : EditorRenderer
{
     protected override void OnElementChanged(ElementChangedEventArgs&lt;Editor&gt; e)
     {
          base.OnElementChanged(e);

          if (Control != null)
          {
              Control.TintColor = UIColor.Green;
      }

   }
}
  1. Register our handlers like below in the MauiProgram.cs:
       .ConfigureMauiHandlers(handlers =&gt;
       {

#if IOS
             handlers.AddHandler(typeof(MyEditor), typeof(MyEditorRenderer));

#endif
       });

  1. Finally, create an instance for the editor control and then run it to check.
&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
x:Class=&quot;MAUIProjectWithRenderer.MainPage&quot;
xmlns:editor=&quot;clr-namespace:CustomRenderer&quot;
&gt;

&lt;VerticalStacklayout&gt;
    &lt;editor:MyEditor /&gt; 
&lt;/VerticalStacklayout&gt;

&lt;/ContentPage&gt;

huangapple
  • 本文由 发表于 2023年6月22日 06:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定