If the WebView is initialized, the next experimental button handler shows the usage of WebResourceRequested event, which allows to deliver the local contents:
private void button1_Click( object sender, EventArgs e )
{
webView.CoreWebView2.AddWebResourceRequestedFilter( "file://demo/*", CoreWebView2WebResourceContext.All );
webView.CoreWebView2.WebResourceRequested += ( s, a ) =>
{
if( a.Request.Uri == "file://demo/test1.html" )
{
string html = @"<html>
<head>
<script>
function OnLoad()
{
try
{
alert( window.sessionStorage);
alert( window.sessionStorage.getItem('myKey') );
window.sessionStorage.setItem('myKey', Date.now() );
}
catch(error)
{
alert(error);
}
}
</script>
</head>
<body onload=""OnLoad()"">
<h1>TEST</h1>
<p>
Hello!
</p>
</body>
</html>";
var ms = new MemoryStream( Encoding.UTF8.GetBytes( html ) );
a.Response = webView.CoreWebView2.Environment.CreateWebResourceResponse( ms, 200, "OK", "Content-Type: text/html" );
}
};
webView.CoreWebView2.Navigate( "file://demo/test1.html" );
}
Accessing the window.sessionStorage object in this sample should not fail. Maybe it will work in your tests too.
For specific details and optimizations, see: