WinRT & UWP WebView localhost url doesn't fire when app is packaged

泪湿孤枕 提交于 2019-12-10 20:58:04

问题


Boy I'm really struggling with this one!

So I have a WinRT Metro application that has an HTML webpage embedded into a webview within the application. In the HTML page there is a div with an href to a localhost url. I'm using this localhost call to communicate with a .NET desktop application that is listening for this url on a localhost port.

When I build the application (as debug or release) in Visual Studio (2015, update 1 or 2), the application works as expected. I can click on the div, the url is fired, and the communication is successful.

However, when I package the application and sideload it on to my machine, the functionality does not work as expected. I can click on the div...but the url is never fired.

HTML Url Example ([...] = code removed):

<a href="http://localhost:8123/?Api [...] >Click here</a>

Codebehind Navigating to HTML Example:

this.webView.Navigate(new Uri("ms-appdata:///local/index.html"));

XAML Webview Instantiation:

<WebView x:Name="webView" Grid.Row="1" ScriptNotify="webView_ScriptNotify" Grid.Column="1" Visibility="Collapsed"/>

Here are the things I've tried:

  1. I created a WinRT app that only contained the webview and the embedded HTML page. Functionality works when built, but not when packaged.
  2. I created a UWP app that only contained the webview and the embedded HTML page. Functionality works when built, but not when packaged.
  3. I tried packaging the app outside of Visual Studio using the command line as described here. The functionality does not work.
  4. I tried using different versions of Visual Studio 2015. Same results.
  5. I tried building/packaging on different machines. Same results.
  6. I tried to navigate to google instead of the localhost. Works.
  7. I tried to navigate to another html file instead of the localhost. Works.
  8. Naturally, I tried the obvious things too like cleaning, changing package name, changing settings in the Visual Studio App Packager, reboots, removing references, etc.

Has anyone experienced this before? It seems to be some sort of bug with the packaging process. Any suggestions/tips/answers are welcome! Thanks!

And just in case you're wondering, this application is intended for enterprise use only within the company I work for and will not be uploaded to the Windows Store.


回答1:


Normally, you can't open a loopback connection in a UWP store app. Full stop. If you're side loading, or in an enterprise environment, there is a workaround:

https://msdn.microsoft.com/en-us/library/windows/apps/dn640582.aspx




回答2:


I had a problem similar enough to this for it to be worth sharing my experience.

My app is a Universal app with a WebView control. The WebView source is a file in the app package. I define my WebView in the XAML as follows.

<WebView x:Name="myWebview" Source="ms-appx-web:///Assets/www/mylocalwebpage.html"/>

In Debug mode this works perfectly. In the first run in Release mode it does not load the page. In all subsequent runs of the Release mode app it works perfectly. It's a reproducible and incredibly frustrating bug.

My workaround is simple but effective. I use a timer to set the Source of the webview after one second. I add the following line below this.InitializeComponent();

// this worksaround a bug: on first launch in release mode the webview doesn't load
loadWebviewTimer = new Timer(loadWebview, null, 1000, Timeout.Infinite);

And then another

private async void loadWebview(object state)
    {
        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            myWebview.Navigate(new Uri("ms-appx-web:///www/mylocalwebpage.html"));
        });
    }

It's not a pretty fix, but it works. Of interest, mylocalwebpage.html has quite a lot of javascript in it; I suspect that this is related to the problem.



来源:https://stackoverflow.com/questions/36629465/winrt-uwp-webview-localhost-url-doesnt-fire-when-app-is-packaged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!