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

前端 未结 1 509
盖世英雄少女心
盖世英雄少女心 2020-12-09 19:23

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

相关标签:
1条回答
  • 2020-12-09 19:59

    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:

    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:

    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%\AppData\Local\Packages\{PackageId}

    which usually is C:\Users\{UserName}\AppData\Local\Packages\{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.

    0 讨论(0)
提交回复
热议问题