[英]Xamarin.Forms Wysiwyg: move project from Xamarion.iOS to Xamarin.Forms using custom renderer
我想使用Xamarin.Forms渲染器将所见即所得编辑器构建为自定义组件。 我找到了一个解决方案,该解决方案是Xamarin.iOS项目,可满足我的需要: https : //github.com/XAM-Consulting/TEditor 。 我按照提到的示例进行操作,并为Android实现了Wysiwyg渲染器,但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
属性的标记
所以,我的问题是:
不知道我是否正确理解了您的问题。 但这是我使用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中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.