Forum Xamarin.Forms

Xamarin forms: How to parse HTML Data having video links?

SreeeeSreeee INMember ✭✭✭✭✭

I have video links(Vimeo video) and normal texts in my HTML data. I am using Xam.Plugin.HtmlLabel for parsing the HTML data. But it is not effective, the links are not clickable on the iPhone. Also, the video links are missing when coming to UI.

I have added my sample HTML data here. I am using a webview for showing videos. So the video links should open on the app itself using webview or any other property. And show the remaining description as normal text. I need a UI like this. How can I do this? My current UI using Xam.Plugin.HtmlLabel.

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You could still use WKWebview to load html string .

    Custom Webview

    public class MyWebView : WebView
    {
        public static readonly BindableProperty UrlProperty = BindableProperty.Create(
            propertyName: "Url",
            returnType: typeof(string),
            declaringType: typeof(MyWebView),
            defaultValue: default(string));
    
        public string Url
        {
            get { return (string)GetValue(UrlProperty); }
            set { SetValue(UrlProperty, value); }
        }
    }
    

    Custom renderer

    [assembly: ExportRenderer(typeof(MyWebView), typeof(MyWebViewRenderer))]
    namespace ppp.iOS
    {
        public class MyWebViewRenderer : ViewRenderer<MyWebView, WKWebView>
        {
            WKWebView _wkWebView;
            protected override void OnElementChanged(ElementChangedEventArgs<MyWebView> e)
            {
                base.OnElementChanged(e);
    
                if (Control == null)
                {
                    var config = new WKWebViewConfiguration();
                    _wkWebView = new WKWebView(Frame, config);
                    SetNativeControl(_wkWebView);
                }
                if (e.NewElement != null)
                {
                    Control.LoadHtmlString(Element.Url,null);   //use this code instead
                    //Control.LoadRequest(new NSUrlRequest(new NSUrl(Element.Url)));
                }
            }
        }
    }
    

    Usage

            MyWebView web = new MyWebView();
    
            web.VerticalOptions = LayoutOptions.FillAndExpand;
            web.Url = "html string";
    
            Content = web;
    
  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX I tried this, but not working. Nothing is showing in UI, only a blank screen is showing in UI.

  • ColeXColeX Member, Xamarin Team Xamurai

    @Sreeee said:
    @ColeX I tried this, but not working. Nothing is showing in UI, only a blank screen is showing in UI.

    Check my project.

    ppp.zip 181.1K
  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX Whenever I tried this on my project, getting below exception. Can you please suggest a solution? When I open the page having MyWebView the page redirects to Main.cs and shows the below exception.


  • ColeXColeX Member, Xamarin Team Xamurai

    Not clear , it works fine on my side .

  • SreeeeSreeee INMember ✭✭✭✭✭

    @ColeX What is the use of Content = web;?

  • ColeXColeX Member, Xamarin Team Xamurai
    edited December 2019

    @Sreeee said:
    @ColeX What is the use of Content = web;?

    Just Gets or sets the view that contains the content of the Page , it's the same as the way set in xaml .

Sign In or Register to comment.