可以从本机自定义呈现器覆盖Xamarin Forms控件属性



我已经为Entry Xamarin Forms控件实现了本机iOS自定义渲染器,其中更改了一些属性,如BackgroundColor。

我需要覆盖自定义渲染器中的一些属性。这可能吗?

你的问题很难理解(你到底想做什么)。在我的XF应用程序中,我在页面上使用编辑器控件
由于iOS上的默认字体大小太小,我实现了一个自定义渲染,为iOS设置了一个更大的字体(尤其是而且仅限于此)。您也可以覆盖其他属性(类似于示例中的字体大小)。

在XF中添加:

public class MG_Editor : Editor // Interface to specific Renderer
{
// only placeholder for interface
}

iOS-项目中添加一个类,如"iOS_Specific.cs":

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
// additionall usings 
//
using MatrixGuide; // your namespace
using MatrixGuide.iOS; // your namespace.iOS
//
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
//
using Foundation;
using UIKit;
[assembly: ExportRenderer(typeof(MG_Editor), typeof(EditorCustomRenderer))]
namespace MatrixGuide.iOS
{
    public class EditorCustomRenderer : EditorRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
        {
            base.OnElementChanged(e);
            if (e.OldElement == null)
            {   // perform initial setup
                // lets get a reference to the native control
                var nativeTextView = (UITextView)Control;
                // do whatever you want to the UITextField here!
                nativeTextView.Font = UIFont.SystemFontOfSize(18);
            }
        }
    }
}

然后在XF代码(例如页面上)中创建控件:

var EditorxxYourWishedNamexx = new MG_Editor();

因此,对于Android和WP,使用标准编辑器控件,而对于iOS,则使用自定义实现(使用较大字体)。备注:
-MatrixGuide是我的应用程序的命名空间
-你可以取一个你喜欢的名字,而不是EditorxxYourWishedNamexx
-类似地,您也可以为其他平台实现自定义渲染器。

最新更新