Script Notify about ms-appdata

I want to notify my web view with a button in an html file and run javascript:

function notify(str) { window.external.notify(str); } 

Event shot using wv_ScriptNotify(..., ...) :

 void wv_ScriptNotify(object sender, NotifyEventArgs e) { Color c=Colors.Red; if (e.CallingUri.Scheme =="ms-appx-web" || e.CallingUri.Scheme == "ms-appdata") { if (e.Value.ToLower() == "blue") c = Colors.Blue; else if (e.Value.ToLower() == "green") c = Colors.Green; } appendLog(string.Format("Response from script at '{0}': '{1}'", e.CallingUri, e.Value), c); } 

I installed the html file on ms-appx-web and it works well, and I understand that the html file should be stored in a local folder. So I change ms-appx-web:///.../index.html to ms-appdata:///local/.../index.html .

Already search the microsoft forum and get this . There is a solution in this thread using resolver, but I'm still confused, how can it notify from javascript, for example, using window.external.notify ? And what event on the C # side will capture a "notification" from javascript other than "ScriptNotify"?


Update

There is a solution from here , for example using a resolver, and he said to use ms-local-stream:// instead of using ms-appdata://local so that I can still use the ScriptNotify event. But unfortunately, the example is using ms-appx , which means using InstalledLocation not LocalFolder .

Trying to search and search the msdn documentation site for ms-local-stream , but the only documentation is only the ms-local-stream format without any examples like ms-local-stream://appname_KEY/folder/file .

Based on this documentation, I made some examples to try:

 public sealed class StreamUriWinRTResolver : 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) { throw new 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> private async Task<IInputStream> getContent(string path) { // We use a package folder as the source, but the same principle should apply // when supplying content from other locations try { // My package name is "WebViewResolver" // The KEY is "MyTag" string scheme = "ms-local-stream:///WebViewResolver_MyTag/local/MyFolderOnLocal" + path; // Invalid path // string scheme = "ms-local-stream:///WebViewResolver_MyTag/MyFolderOnLocal" + path; // Invalid path Uri localUri = new Uri(scheme); StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read); return stream.GetInputStreamAt(0); } catch (Exception) { throw new Exception("Invalid path"); } } } 

And inside my MainPage.xaml.cs:

 protected override void OnNavigatedTo(NavigationEventArgs e) { // The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name // and an application-defined key, which identifies the specific resolver, in this case 'MyTag'. Uri url = wv.BuildLocalStreamUri("MyTag", "index.html"); StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver(); // Pass the resolver object to the navigate call. wv.NavigateToLocalStreamUri(url, myResolver); } 

It always gets an exception when it reaches the line StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); .

If anyone has ever received this problem and already solved it, consult.

+6
source share
1 answer

After debugging, I found something interesting, the BuildLocalStreamUri part already does ms-local-stream automatically.

I made some changes to the getContent method inside the StreamUriWinRTResolver class:

 public sealed class StreamUriWinRTResolver : 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) { throw new 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> private async Task<IInputStream> getContent(string path) { // We use a package folder as the source, but the same principle should apply // when supplying content from other locations try { // Don't use "ms-appdata:///" on the scheme string, because inside the path // will contain "/local/MyFolderOnLocal/index.html" string scheme = "ms-appdata://" + path; Uri localUri = new Uri(scheme); StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read); return stream.GetInputStreamAt(0); } catch (Exception) { throw new Exception("Invalid path"); } } } 

Change the file path to MainPage.xaml.cs:

 protected override void OnNavigatedTo(NavigationEventArgs e) { // The 'Host' part of the URI for the ms-local-stream protocol needs to be a combination of the package name // and an application-defined key, which identifies the specific resolver, in this case 'MyTag'. Uri url = wv.BuildLocalStreamUri("MyTag", "/local/MyFolderOnLocal/index.html"); StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver(); // Pass the resolver object to the navigate call. wv.NavigateToLocalStreamUri(url, myResolver); wv.ScriptNotify += wv_ScriptNotify; } protected override void wv_ScriptNotify(object sender, NavigationEventArgs e) { if (e.CallingUri.Scheme == "ms-local-stream") { // Do your work here... } } 
+7
source

Source: https://habr.com/ru/post/954065/


All Articles