I am trying to show a web page relative to the local folder in a UWP app. This means, I have the page itself as string, but assets (images, scripts) are located in the apps local folder.
I tried to use the NavigateToString
method that as I understood should serve this task, in combination with the Source
-property of a WebView
control.
var baseUri = new Uri("ms-appdata:///local/");
WebView.Source = baseUri;
var page = ...;
WebView.NavigateToString(page);
However, this gives me the following error message when assigning the Source
-property of the web view:
Exception thrown: 'System.ArgumentException' in App.exe
Additional information: Value does not fall within the expected range.
So apparently, using the local app folder for assets is not supported?
The app targets minimum Windows 10 Build 10586.
Does anybody know whether there is a useful workaround for this?
Edit: I tried to make a minimum example. In the following, I created a small test app that does nothing but loading a static html file from the local storage. As far as I can tell, this code exactly resembles the XamlWebView example but does raise an ArgumentException.
MainPage.xaml
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView Name="WebView" />
</Grid>
</Page>
MainPage.xaml.cs
using System;
using System.IO;
using Windows.Storage;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace App1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
Initialize();
}
private async void Initialize()
{
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("index.html", CreationCollisionOption.ReplaceExisting);
using (var sw = new StreamWriter(await file.OpenStreamForWriteAsync()))
{
sw.WriteLine("<html>");
sw.WriteLine("<body>");
sw.WriteLine("This is some text");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
}
WebView.Navigate(new Uri("ms-appdata:///local/index.html"));
}
}
}
I do not even know where the difference to the sample is, can't find any.