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

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

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

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.

Please create a new report for Bug 57451 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Gordon Brown 2017-06-13 19:09:53 UTC
Linked is a slightly modified version of the solution for the Hanselman.Forms sample.

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 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   BAD
2.3.5-pre3  BAD

### Version Info
Microsoft Visual Studio Enterprise 2017 
Version 15.2 (26430.13) Release
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 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 (fec6f88)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

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

Xamarin.iOS and Xamarin.Mac SDK (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 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))
                LoadHtml(html, baseUrl);
Comment 4 soernt.poppe 2017-07-03 14:31:21 UTC

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

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