【问题标题】:Xamarin.Forms Wysiwyg: move project from Xamarion.iOS to Xamarin.Forms using custom rendererXamarin.Forms Wysiwyg:使用自定义渲染器将项目从 Xamarin.iOS 移动到 Xamarin.Forms
【发布时间】:2017-10-01 08:44:11
【问题描述】:

我想使用 Xamarin.Forms 渲染器将所见即所得编辑器构建为自定义组件。我找到了一些解决方案,它是一个 Xamarin.iOS 项目,它可以满足我的需要: https://github.com/XAM-Consulting/TEditor。 我遵循提到的示例并为 Android 实现了所见即所得的渲染器,但我在使用 iOS 渲染器时遇到了问题。 这是我重写的ViewRenderer<WysiwygEditor, UIView> 方法:

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 的 HTML 包含一个带有contenteditable=true 属性的标签

所以,我的问题是:

  1. 如何在我的项目中将 TEditorViewController 从示例正确移动到 viewrenderer?
  2. ViewRenderer 中名为 ViewController 的属性的目的是什么?我需要覆盖它吗?

【问题讨论】:

  • 您是否将TEditor NuGet 包安装到您的共享、iOS 和 Android 项目中?可能比尝试自己复制所有文件更容易。
  • 不,我不想安装这个包,因为我需要做很多自定义,这就是为什么最好从 TEdior 中获得这个想法并在我的项目中实现它

标签: c# xamarin.ios xamarin.forms wysiwyg custom-renderer


【解决方案1】:

不确定我是否正确理解了您的问题。但这是我使用 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 中。

【讨论】:

  • 我做到了,但没有我期望的结果。它不起作用,因为当我单击格式化控件时键盘正在关闭,但在 TEditor 中它没有关闭
猜你喜欢
  • 2017-01-07
  • 2016-10-24
  • 1970-01-01
  • 2014-08-29
  • 2016-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
相关资源
最近更新 更多