Skip to content Skip to sidebar Skip to footer

How To Javascript Notify() With Ms-appdata On Windows 10 Uwp

Due to file-writing security issues we have tried to switch our app from using ms-appx-web: to using ms-appdata:. But it immediately fails because we rely on window.external.notify

Solution 1:

But it immediately fails because we rely on window.external.notify() which works fine with ms-appx-web: but seems to behave as a no-op with ms-appdata:.

For your scenario please use the scheme ms-local-stream:///, rather than ms-appdata:///. And I have tested ms-local-stream:/// scheme, it is working pretty well.

To use the NavigateToLocalStreamUri method, you must pass in an IUriToStreamResolver implementation that translates a URI pattern into a content stream. Please reference the following code.

StreamUriWinRTResolver

publicsealedclassStreamUriWinRTResolver : IUriToStreamResolver
    {
        ///<summary>/// The entry point for resolving a Uri to a stream.///</summary>///<param name="uri"></param>///<returns></returns>public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
        {
            if (uri == null)
            {
                thrownew Exception();
            }
            string path = uri.AbsolutePath;
            // Because of the signature of this method, it can't use await, so we// call into a separate helper method that can use the C# await pattern.return getContent(path).AsAsyncOperation();
        }

        ///<summary>/// Helper that maps the path to package content and resolves the Uri/// Uses the C# await pattern to coordinate async operations///</summary>privateasync Task<IInputStream> getContent(string path)
        {
            // We use a package folder as the source, but the same principle should apply// when supplying content from other locationstry
            {
                Uri localUri = new Uri("ms-appdata:///local" + path);
                StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
                IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
                return stream.GetInputStreamAt(0);
            }
            catch (Exception) { thrownew Exception("Invalid path"); }
        }
    }

MainPage

publicMainPage()
 {
     this.InitializeComponent();
     MyWebView.ScriptNotify += MyWebView_ScriptNotify;
     Uriurl= MyWebView.BuildLocalStreamUri("MyTag", "/Test/HomePage.html");
     StreamUriWinRTResolvermyResolver=newStreamUriWinRTResolver();
     MyWebView.NavigateToLocalStreamUri(url, myResolver);
 }

I have uploaded the code sample to github. Please check.

Post a Comment for "How To Javascript Notify() With Ms-appdata On Windows 10 Uwp"