2017-05-03 99 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

相關問題