Script Notify for ms-appdata

Je souhaite notifier ma vue web à partir du bouton dans le fichier html et déclencher le javascript:

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

L'événement a été capturé en utilisant 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); } 

Je définis le fichier html sur ms-appx-web et ça marche bien, et je me rends compte que le fichier html doit être stocké dans un dossier local. Donc, je change le ms-appx-web:///.../index.html à ms-appdata:///local/.../index.html .

Déjà recherchez dans le forum Microsoft et obtenez ceci . Sur ce fil, il y a une solution à l'aide du résolveur, mais je suis toujours confuse, comment peut-il notifier de javascript comme using window.external.notify ? Et quel genre d'événement dans le côté C # qui va capturer la "notification" de javascript autre que "ScriptNotify"?


Mettre à jour

Il existe une solution à partir d' ici , exemple en utilisant le résolveur et il a dit d'utiliser ms-local-stream:// plutôt que d'utiliser ms-appdata://local donc je peux toujours utiliser l'événement ScriptNotify . Mais malheureusement, l'exemple utilisant ms-appx signifie l'utilisation de LocalFolder non le LocalFolder .

En essayant de googler et de rechercher dans le site msdn pour la documentation pour ms-local-stream mais la seule documentation est juste le format de ms-local-stream sans aucun exemple comme ms-local-stream://appname_KEY/folder/file .

À partir de cette documentation, j'ai fait un échantillon pour l'essayer:

 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"); } } } 

Et à l'intérieur de mon 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); } 

Il obtient toujours l'exception lorsqu'elle atteint le StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); ligne.

Si quelqu'un a déjà eu ce problème et l'a déjà résolu, veuillez en informer.

Après avoir débogué, j'ai trouvé quelque chose d'intéressant, la partie BuildLocalStreamUri fait déjà le ms-local-stream automatiquement.

J'ai apporté quelques modifications sur la méthode StreamUriWinRTResolver classe StreamUriWinRTResolver :

 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"); } } } 

Modifiez le chemin du fichier sur 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... } }