Rameshwar Vyevhare
Rameshwar Vyevhare

Reputation: 2759

webview implementation using MvvmCross android native XAMARIN

We are using mvvmcross for Xamarin android native but unable to use webview to render html page, please help if anyone tried ...regular xamarin android can do but since we use mvvmcross then that regular won't work

We tried using mvvmcross XAMRIN also added plugin https://nuget.info/packages/MvvmCross.Plugin.WebBrowser/8.0.2 but nothing works

Upvotes: 1

Views: 225

Answers (3)

Kiran Khalkar
Kiran Khalkar

Reputation: 66

I Did it from View because below LoadDataWithBaseURL() function is not supported/working in ViewModel i have added below code in View.

WebView.LoadDataWithBaseURL(null, Html, "text/html", "utf-8", null); 

In design

<WebView android:id="@+id/ReceiptWebView" android:layout_width="match_parent" android:layout_height="match_parent"/>

It will load HTMl with Table.

Upvotes: 1

Trevor Balcom
Trevor Balcom

Reputation: 3888

This should get you started. I've tested with MvvmCross 8.0.2. This code is slightly modified from Google

You could also probably just use the MvvmCross Web Browser plugin by first saving your HTML to a file then using a "file://..." URI.

using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Webkit;
using System;

namespace MyApp.Droid.Ui.Controls
{
    [Register("myapp.droid.ui.controls.MvxWebView")]
    public class MvxWebView : WebView
    {
        private string _text;

        [Preserve(Conditional = true)]
        public MvxWebView(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        [Preserve(Conditional = true)]
        public string Text
        {
            get
            { 
                return _text;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    return;
                }

                _text = value;

                LoadData(_text, "text/html", "utf-8");
                UpdatedHtmlContent();
            }
        }

        public event EventHandler HtmlContentChanged;

        private void UpdatedHtmlContent()
        {
            HtmlContentChanged?.Invoke(this, EventArgs.Empty);
        }
    }
}

Then in your view:

    <myapp.droid.ui.controls.MvxWebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:MvxBind='Text MyHtml' />

...and in your ViewModel:

public string MyHtml
{
    get => _myHtml;
    set => SetProperty(ref _myHtml, value);
}

MyHtml = "<html><p>Hello, World!</p></html>";

Upvotes: 1

DevenCC
DevenCC

Reputation: 496

Not necessarily a solution to fix the non-working WebView; but if indeed this is an outstanding compatibility issue, maybe looking into MvvmsCross' WebBrowser plugin might be a suitable workaround fir your application.

Upvotes: 0

Related Questions