1
votes

I'm working with WebView on Xamarin Forms 1.4.2 The problem I have seems to be a binding issue. But I'm not sure why this happens.

In my OnAppearing() event, I fetch data from SQLite Local Database and fill it into the WebView.

The content of webview is not been shown the first time the page loads.

I have a ListView under the WebView. On selecting any item in the listview the content of webview shows.

Has anyone experience this? Is there a work around?

i bind my webview directly to a HTML string :

webview.Source = new HtmlWebViewSource
{
     Html = @"<html>
                    <head>
                          <meta name='viewport' content='width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'/>
                    </head>
                    <body height='100%'>"
                    + htmlstring
                    + @"</body>
               </html>"
};
2

2 Answers

1
votes

This is because of your line

<meta name='viewport' content='width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'/>

Correct it, or for now remove it and check after removing.

0
votes

I've tried the following example and it works for me.

Depending on what you do in the OnAppearing you may have to wrap the update using Xamarin.Forms.Device.BeginInvokeOnMainThread(()=>{...});. I've included it, commented out, in the example below:-

XAML:-

<StackLayout>
    <WebView x:Name="webView1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>    
    <Button x:Name="cmdLoadTestHtml1" Text="Load Test Html 1" Clicked="cmdLoadTestHtml1_Clicked"/>
</StackLayout>

Code Behind:-

    protected override void OnAppearing()
    {
        base.OnAppearing();
        //
        LoadTestHtml("OnAppearing....");
        //
        // Alternatively try this:-
        //Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
        //    {
        //        LoadTestHtml("OnAppearing.");
        //    });
    }

    void cmdLoadTestHtml1_Clicked(object sender, EventArgs e)
    {
        LoadTestHtml("Button clicked");
    }

    void LoadTestHtml(string pstrSomeInnerContentToDisplay)
    {
        webView1.Source = new HtmlWebViewSource
        {
            Html = @"<html>
                <head>
                      <meta name='viewport' content='width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;'/>
                </head>
                <body height='100%'>"
                           + pstrSomeInnerContentToDisplay
                           + @"</body>
           </html>"
        };
    }