我想使用Xamarin.Forms渲染器构建一个wysiwyg编辑器作为自定义组件。我发现了一些解决方案,这是一个Xamarin.iOS项目,它可以满足我的需求: https://github.com/XAM-Consulting/TEditor。 我后面提到的例子,并为Android实现Wysiwyg渲染器,但我有iOS渲染器的问题。 这是我的ViewRenderer<WysiwygEditor, UIView>
覆盖方法:Xamarin.Forms Wysiwyg:将项目从Xamarion.iOS移动到Xamarin.Forms使用自定义渲染器
protected override void OnElementChanged(ElementChangedEventArgs<WysiwygEditor> e)
{
base.OnElementChanged(e);
if (Control == null)
{
_view = new UIView();
_editorWebView = new UIWebView();
_view.Add(_editorWebView);
var interpretter = new JavaScriptEnterpretter(_editorWebView);
_jsApi = new RichEditorApi(interpretter);
_editorWebView.Delegate = new WysiwygWebViewClient(_jsApi, Element.Html);
_editorWebView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth
| UIViewAutoresizing.FlexibleHeight
| UIViewAutoresizing.FlexibleTopMargin
| UIViewAutoresizing.FlexibleBottomMargin;
_editorWebView.KeyboardDisplayRequiresUserAction = false;
_editorWebView.ScalesPageToFit = true;
_editorWebView.BackgroundColor = UIColor.White;
_editorWebView.ScrollView.Bounces = false;
_keyboardDidFrameToken = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidChangeFrameNotification, KeyboardDidFrame);
_keyboardWillShowToken = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, KeyboardWillShowOrHide);
_keyboardWillHideToken = NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, KeyboardWillShowOrHide);
BuildToolbar(Element.Toolbars);
interpretter.Init();
_jsApi.SetPlatformAsIOS();
SetNativeControl(_view);
}
if (e.OldElement != null)
{
e.OldElement.Cleanup();
}
if (e.NewElement != null)
{
e.NewElement.Initialize(_jsApi);
SetHtml("some html");
}
}
WysiwygEditor是我的类继承Xamarin.Forms.View。 问题是,JavaScript调用不起作用(如_editorWebView.EvaluateJavascript("document.execCommand('bold', false, null)")
)。其装入_editorWebView
包含contenteditable=true
标签属性
所以HTML,我的问题是:
- 如何TEditorViewController从例如移动到我的项目正确的ViewRenderer?
- 在ViewRenderer中名为ViewController的属性的目的是什么?我是否需要重写它?
您是否已将[TEditor](https://www.nuget.org/packages/TEditor/)NuGet软件包安装到您的共享,iOS和Android项目中?可能比试图将所有文件复制到自己更容易。 – hvaughan3
不,我不想安装这个软件包,因为我需要做很多自定义设置,这就是为什么最好从TEdior中获取想法并在我的项目 – Alexander