Bug 57451 - WebView UWP not displaying
Summary: WebView UWP not displaying
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Windows (show other bugs)
Version: 2.3.5
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-06-13 19:09 UTC by Gordon Brown
Modified: 2018-05-14 23:11 UTC (History)
9 users (show)

See Also:
Tags: ac webview uwp
Is this bug a regression?: ---
Last known good build:


Attachments
Create dynamic HTML and try to display via HtmlWebViewSource. (146.34 KB, application/x-zip-compressed)
2017-07-03 14:35 UTC, soernt.poppe
Details

Description Gordon Brown 2017-06-13 19:09:53 UTC
Linked is a slightly modified version of the solution for the Hanselman.Forms sample.
https://1drv.ms/u/s!AmeCKE6dgpGln8ZlXlYGrTLV7o0FZg

I've added a "Repro" page and modified the About Page to contain a WebView.

The original issue was seen on a stable version of forms.  
The linked version is based on the 2.3.5.239-pre3 forms package in the chance that something was fixed that might alleviate the problem. 

To reproduce the problem, 
build the project (debug or release) for x64 UWP and deploy to the local machine.  
I'm building on VS 2017 Community and am deploying to a pc running Windows 10 v1703 OS Build 15063.332.

run the app.  
At startup the app displays the added "Repro" page. 
Sometimes the webview is displayed, other times it doesn't.
See the screenshots in the Screenshots folder for success and fail captures.

Usually, if the webview does display on the Repro page, going to the About page will demonstrate the issue there. 
Switching between the windows eventually causes both to display as expected.  

The problem does not present on the Android or iOS versions of the app.
Comment 1 Jimmy [MSFT] 2017-06-14 20:36:15 UTC
I was able to reproduce the issue using the project linked to in the description. As mentioned, the issue happens a majority of the time, but not always. I am confirming the report so this can be investigated further.

In the repro project, the WebView is loading HTML with a HtmlWebViewSource so I decided to test loading a website instead. When a loading a website the issue appeared to not occur so this may indicate the issue being related to using a HtmlWebViewSource.


### Version Tests
2.3.6.109   BAD
2.3.5-pre3  BAD


### Version Info
Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.13) Release
VisualStudio.15.Release/15.2.0+26430.13
Microsoft .NET Framework
Version 4.7.02046

Installed Version: Enterprise

Visual Basic 2017   00369-60000-00001-AA107
Microsoft Visual Basic 2017

Visual C# 2017   00369-60000-00001-AA107
Microsoft Visual C# 2017

Visual C++ 2017   00369-60000-00001-AA107
Microsoft Visual C++ 2017

Visual F# 4.1   00369-60000-00001-AA107
Microsoft Visual F# 4.1

Application Insights Tools for Visual Studio Package   8.6.00404.2
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017   15.0.30503.0
ASP.NET and Web Tools 2017

ASP.NET Web Frameworks and Tools 2017   5.2.50303.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   15.0.30209.0
Azure App Service Tools v3.0.0

Common Azure Tools   1.9
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

JavaScript Language Service   2.0
JavaScript Language Service

JavaScript Project System   2.0
JavaScript Project System

JavaScript UWP Project System   2.0
JavaScript UWP Project System

KofePackagePackage Extension   1.0
KofePackagePackage Visual Studio Extension Detailed Info

Merq   1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2017 - v2.9.50131.1

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   Mono.Debugging.VisualStudio
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   4.2.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

SQL Server Data Tools   15.1.61702.140
Microsoft SQL Server Data Tools

TypeScript   2.2.2.0
TypeScript tools for Visual Studio

Visual Studio Tools for Universal Windows Apps   15.0.26430.06
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Xamarin   4.5.0.486 (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.3.1.2 (9dbc4c5)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.10.0.37 (ad35de4)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 2 Taylor Buchanan 2017-06-22 17:28:20 UTC
Just ran into the same issue here on 2.3.4.247. HtmlWebViewSource doesn't display any of the provided HTML. It's strange because it was working before when I was building in VS 2015. I've since upgraded Windows 10 Creators update and VS 2017, but now I can't get it to work with any version of Xamarin Forms.
Comment 3 Taylor Buchanan 2017-06-22 22:10:49 UTC
I have no idea what is going on here. It seems to have something to do with the UWP WebViewRenderer calling Control.NavigateToString from within the internalWebView.NavigationCompleted event handler. I've tried wrapping NavigateToString with Dispatcher.RunAsync, but that doesn't help. Calling NavigateToString from the Loading and Loaded events result in the same problem. However, the NavigationStarting event works fine. Need some insight from Microsoft on this one.

Here's my current workaround:

    internal sealed class CustomWebViewRenderer : WebViewRenderer, IWebViewDelegate
    {
        void IWebViewDelegate.LoadHtml(string html, string baseUrl)
        {
            if (Element.Source is HtmlWebViewSource && string.IsNullOrEmpty(baseUrl))
                Control.NavigateToString(html);
            else
                LoadHtml(html, baseUrl);
        }
    }
Comment 4 soernt.poppe 2017-07-03 14:31:21 UTC
Hi,

I have the very same problem. The HTML is loaded via a HtmlWebViewSource. The HTML is not displayed. The work around from Taylor Buchanan did not work for me.

I notice that when rotating the device, the HTML will be displayed.
Comment 5 soernt.poppe 2017-07-03 14:35:37 UTC
Created attachment 23263 [details]
Create dynamic HTML and try to display via HtmlWebViewSource.

Have a look at the HtmlPage.cs which tries to display a dynamic created HTML.
Comment 6 soernt.poppe 2017-07-03 14:41:19 UTC
As an addition. 
It will never work in relase mode without an attached debugger.
It will work in release mode when an debugger is attached.
It will work in debug mode (with or without attached debugger).

Hope that helps fixing the bug.
Comment 7 philip.exner 2017-07-05 13:43:55 UTC
Hi. We have the same problem as described by Soernt. Is there any solution for this?
Comment 8 Taylor Buchanan 2017-07-05 15:42:33 UTC
Soernt,

The workaround I provided works fine for me and the OP. In the project you attached, the custom renderer is registered incorrectly.

    [assembly: ExportRenderer(typeof(WebView), typeof(WebViewRenderer))]

should be

    [assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]

After fixing this, the project works as expected.
Comment 9 Joel 2017-11-16 21:31:21 UTC
Is there a fix for this yet? Is there a workaround I can implement in my Xamarin.Forms project?
Comment 10 Michael James 2018-05-14 23:10:13 UTC
I still found I was experiencing this issue today. The work around Taylor mentions I found wouldn't load local resources (styles, javascript etc) so I dug a bit more and found the ContentLoaded event never fires. I was then able to look to see if this event had fired after X milliseconds and simply load the HTML again to fix the issue.

I wrote up the details if its helpful to anyone else: https://blog.mjjames.co.uk/2018/05/uwp-webview-intermittently-fails-to.html
Comment 11 Michael James 2018-05-14 23:11:51 UTC
If easier, the Gist for the solution is available at: https://gist.github.com/mjjames/e0c2e261ba4095ec5a1b51253983a608

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Note You need to log in before you can comment on or make changes to this bug.