如何在Xamarin表单中创建带有或不带有Web视图的RTF文本框? [英] How to create a Rich Text box with or without web view in xamarin forms?

查看:183
本文介绍了如何在Xamarin表单中创建带有或不带有Web视图的RTF文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在xamarin表单中创建带有或不带有Web视图的RTF文本框.我尝试了很多例子,但没有一个适合我.例子是

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

但是Adam.Else的代码给了我很多错误,并且没有用.我已经报告了stackoverflow中的错误.如您在下面看到的此链接-

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-

未处理的例外:Xamarin. Forms.Xaml.XamlParseException:位置12:21.找不到关键的FromRTFConverter的StaticResource

我不知道如何解决此问题.有什么建议吗?

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

推荐答案

我是使用网络视图完成的.首先,我下载了使用HTML/CSS和JavaScript创建的Rich文本框. 这是Rich文本框的链接我将在我的xamarin表单应用程序中将其用作Web视图.

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();
        }
    }

我创建了一个名为 IBaseUrl 的界面.这是我的代码

I created a interface called IBaseUrl. This is my code

public interface IBaseUrl { string Get(); }

然后我在Native中创建了一个名为BaseUrl_Android的类,用于调用已放入android的Assets文件夹中的HTML文件.这是我的 BaseUrl_Android

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";
        }
    }
}

将HTML和JavaScript文件添加到资产文件夹中.如果您对以xamarin形式添加和使用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.

这是我的Rich文本框的屏幕截图

这篇关于如何在Xamarin表单中创建带有或不带有Web视图的RTF文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆