![](/img/trans.png)
[英]Is it possible to write a Xamarin Forms Control Renderer that contains a Subclassed Native Control?
[英]Is possible to override Xamarin Forms control properties from native custom renderer?
我已经为Entry Xamarin Forms控件实现了本地iOS自定义渲染器,其中更改了一些属性,例如BackgroundColor。
我需要重写自定义渲染器的某些属性。 这可能吗?
您的问题很难理解(您到底想做什么)。 在我的XF应用程序中,我在页面上使用Editor-Control。
由于iOS上的default-fontsize太小,我实现了一个自定义渲染器来为iOS设置一个更大的字体(尤其是唯一的字体)。 您还可以覆盖其他属性(类似于示例中的fontsize)。
在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代码(页面上的EG)中创建控件:
var EditorxxYourWishedNamexx = new MG_Editor();
因此,对于Android和WP,使用标准的Editor-control,而对于iOS,使用自定义实现(具有较大的字体)。 笔记:
-MatrixGuide是我的应用程序的名称空间。
-您可以使用自己喜欢的名称来代替EditorxxYourWishedNamexx。
-类似,您还可以为其他平台实现自定义渲染器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.