How to create a Rich Text box with or without web view in xamarin forms?

一个人想着一个人 提交于 2020-01-23 12:27:46

问题


I want create a Rich Text box with or without web view in xamarin forms. I tried many examples but none of that worked for me. The examples are

https://github.com/XAM-Consulting/TEditor

The problem with this example was, it is too slow for me and it was not editing my text at all. And it was getting crash numerous times. So I tried to follow this example

https://forums.xamarin.com/discussion/95318/rich-text-box-in-xamarin-forms

but the code by Adam.Else gave me lots of bugs and it was not working. I have reported the bug in stackoverflow. As you can see this link below-

Unhandled Exception: Xamarin.Forms.Xaml.XamlParseException: Position 12:21. StaticResource not found for key FromRTFConverter occurred

I don't know how to fix this issue. Any suggestions?


回答1:


I did it using web view. First I downloaded a Rich text box which was created using HTML/CSS and JavaScript. This is the link for the Rich text box which I'm going to use as the web view in my xamarin forms app.

MainPage.xaml

<StackLayout>
        <!-- Place new controls here -->

        <WebView x:Name="MyWebView"  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
    </StackLayout>

MainPage.xaml.cs

 public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            MyWebView.Source = new HtmlWebViewSource();
            MyWebView.Source = DependencyService.Get<IBaseUrl>().Get();
        }
    }

I created a interface called IBaseUrl. This is my code

public interface IBaseUrl { string Get(); }

And I created a class in Native called BaseUrl_Android for calling the Html file which I have put in my Assets folder in android. This is my code for BaseUrl_Android

[assembly: Dependency(typeof(BaseUrl_Android))]
namespace webviewdemo.Droid
{
    public class BaseUrl_Android : IBaseUrl
    {
        public string Get()
        {
            return "file:///android_asset/fontawesome5.html";
        }
    }
}

Add your HTML and JavaScript file in assets folder. If you have any doubt in adding and using html file in xamarin forms refer this link. This is all you have to do to get an awesome Rich text box in Xamarin Forms.

This is the screenshot of my Rich text box



来源:https://stackoverflow.com/questions/54070344/how-to-create-a-rich-text-box-with-or-without-web-view-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!