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

hariprasad gudimalla 1 Reputation point
2021-03-18T16:36:24.66+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,323 questions
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. AryaDing-MSFT 2,916 Reputation points
    2021-03-24T05:41:07.187+00:00

    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.