2017-05-03 102 views
0

我想使用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,我的问题是:

  1. 如何TEditorViewController从例如移动到我的项目正确的ViewRenderer?
  2. 在ViewRenderer中名为ViewController的属性的目的是什么?我是否需要重写它?
+0

您是否已将[TEditor](https://www.nuget.org/packages/TEditor/)NuGet软件包安装到您的共享,iOS和Android项目中?可能比试图将所有文件复制到自己更容易。 – hvaughan3

+0

不,我不想安装这个软件包,因为我需要做很多自定义设置,这就是为什么最好从TEdior中获取想法并在我的项目 – Alexander

回答

0

不知道我是否正确理解你的问题。但这是我使用Xamarin.iOS项目中的现有ViewController的方式。

如果您想使用ViewRenderer(而不是PageRenderer),我只需初始化ViewController,然后在“SetNativeControl(...)”中使用它的View。

TEditorViewController TController; 
protected override void OnElementChanged(ElementChangedEventArgs<WysiwygEditor> e) 
{ 
    //Don´t call the base method here, since you want to create your own view. 
    if (Control == null) 
    { 
     //Initialize TController 
     TController = new TEditorViewController(); 
     //etc. 
     SetNativeControl(TController.View); 
    } 
} 

通过这种方式,您可以使用整个控制器并将其包装到Forms Renderer中。

+0

中实现它,但是没有结果我预计。它不起作用,因为当我点击格式控制时键盘正在关闭,但在TEditor中它没有关闭 – Alexander

相关问题