question

hariprasadgudimalla-2986 avatar image
0 Votes"
hariprasadgudimalla-2986 asked AryaDing-MSFT commented

Uwp app unable to load local htm file from local storage folder

79414-uwp-issue.jpegHi everyone, We need to load the localhtml file(i.e) index.htm file, where it loads all other files in UWP applications using webview. As files are downloading to localstate, using the below code in UWP Url = new Uri("ms-appdata:///local/applicationame/index.html").OriginalString But when tried to click on any child pages we are getting popup message as "You will need a new app to open this ms-local-stream link" issue same file is opening with edge browser ​only facing the issue with UWP app only Could you please help us on this.


dotnet-xamarinwindows-uwp
uwp-issue.jpeg (113.5 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Could you share the code that how to load the html file?

0 Votes 0 ·

We are using the below code to load the htm file.

var source = new UrlWebViewSource
{
Url = new Uri("ms-appdata:///local/Applicationfolder/index.htm").OriginalString
`` };

webview.Source = source;

0 Votes 0 ·

Hi NicoZhu,

Could you please let me know your suggestion or let me know your available time so that we can contact you to have the discussion

0 Votes 0 ·

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered AryaDing-MSFT commented

Hi,

Welcome to Microsoft Q&A!

Derive from official document, to display local content using a WebView, you'll need to open the HTML file like any other, then load the contents as a string into the Html property of an HtmlWebViewSource. In a world, you need to set the BaseUrl property on the HtmlWebViewSource used by the WebView.

You could refer to the following steps to do this, more info could be found in the offical document.

  1. Place html file in the project root with the build action set to Content

  2. Create the IBaseUrl Interface

  3. Create the implementation of IBaseUrl


LocalHtml.cs:

 using WebViewC;
 using Xamarin.Forms;
 [assembly: Dependency(typeof(BaseUrl))]
 namespace WebViewC
 {
     public class LocalHtml:ContentPage
     {
         public LocalHtml()
         {
             var webView = new Xamarin.Forms.WebView();
             var htmlSource = new UrlWebViewSource();
             htmlSource.Url = DependencyService.Get<IBaseUrl>().Get() + "htmldoc.html";
             webView.Source = htmlSource;
             Content = webView;
         }
    
     } 
     public class BaseUrl : IBaseUrl
     {
         public string Get()
         {
             return "ms-appx-web:///";
         }
     }
 }

App.xaml.cs:

 public App()
         {
             InitializeComponent();
             MainPage = new LocalHtml();        
         }

IBaseUrl.cs

 namespace WebViewC
 {
     internal interface IBaseUrl
     {
         string Get();
     }
 }



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.






· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your reply.

As per our requirement, we need to show the downloaded content. So we are downloading the website content as zipfiles into the local folders and extracting the content. Every thing we are doing at runtime and we are not maintaining any static files in the project folder initially.
Firstly we need to load the index.htm file which will have link to other pages. This Code " Url = new Uri("ms-appdata:///local/applicationame/index.html").OriginalString " is working well. But when there is internal navigation within the pages (i.e while navigating to child pages ) the page remains in loading state and it is giving me the popup saying "you will need a new app to open this ms-local-stream-link".

0 Votes 0 ·
AryaDing-MSFT avatar image AryaDing-MSFT hariprasadgudimalla-2986 ·

@hariprasadgudimalla-2986 I have tested it and it works well. You need to make sure that these pages are placed in the same folder. I suggest you put them in Assets folder.

0 Votes 0 ·

Thanks for your reply, as I mentioned early, we are downloading the entire website related files in zip format at runtime and extracting them
So we don't have any static files to be placed in assets folder. Every thing we are doing dynamically. Please find below the folder structure which we are downloading and extracting them at runtime.

83218-folderstructure.png



Same folder content files, we are able to load in Android and iOS. Except in UWP platform..

0 Votes 0 ·
folderstructure.png (59.5 KiB)
Show more comments