Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

c# - Loading Html file in WebView in xaml in UWP from app data local folder

I have a requirement where I need to load an html file from app data folder in xaml WebView in UWP. Html file is also referencing different Js files in another folder ("99/js/"). Any one with UWP knowledge guide me. Thanks in advance I am using following code, Browser is my WebView.

  var Uri = new Uri("ms-appdata:///Local/Downloads/99/index.html");
  Browser.Navigate(Uri);

My folder structure in 99 folder is: udapte
I am trying load html file in offline to WebView which is not loading same html file is loading with server url.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To load the index.html in WebView while offline, you need to make sure all the resource used in index.html are correctly located in app's LocalFolder. And all these content must be placed in a subfolder under the local folder.

Ref Remarks in WebView class:

To load uncompressed and unencrypted content from your app’s LocalFolder or TemporaryFolder data stores, use the Navigate method with a Uri that uses the ms-appdata scheme. The WebView support for this scheme requires you to place your content in a subfolder under the local or temporary folder. This enables navigation to URIs such as ms-appdata:///local/folder/file.html and ms-appdata:///temp/folder/file.html . (To load compressed or encrypted files, see NavigateToLocalStreamUri.)

For example, I created a simple index.html that use index.js in js folder and index.css in css folder.

index.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="js/index.js"></script>
    <link href="css/index.css" rel="stylesheet" />
</head>
<body>
    <button id="myBtn">Click Me!</button>
    <div id="myDiv"></div>
</body>
</html>

index.js

window.onload = function () {
    document.getElementById("myBtn").onclick = function () {
        document.getElementById("myDiv").innerHTML += "You have clicked once! <br>";
    }
}

index.css

#myDiv {
    border: 2px dotted black;
    width: 500px;
    height: 500px;
}

They located in my app's LocalFolder like following:
enter image description here

Then in my UWP app, I used following code:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <WebView x:Name="Browser" />
</Grid>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Browser.Navigate(new Uri("ms-appdata:///local/Downloads/index.html"));
}

This works well:
enter image description here
So if your html file is not loading, please check your app's LocalFolder and make sure your html file and the resources are located in right place.

On Local Machine, the data files are stored in the folder

%USERPROFILE%AppDataLocalPackages{PackageId}

which usually is C:Users{UserName}AppDataLocalPackages{PackageId}, where {UserName} corresponds to the Windows user name and {PackageId} corresponds to the Windows Store application package identifier which you can find as Package family name in Packaging tab of your app's manifest file. The LocalState folder inside the package folder is the LocalFolder.

For Mobile Emulator, we can use some tools like IsoStoreSpy or Windows Phone Power Tools to check the LocalFolder.

If you can load the html file, but some resources are missing like missing the css style, you may need to check your html code and make sure the references are right.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...