Script Notify para ms-appdata
Quiero notificar a mi vista web desde el botón en el archivo html y activar el javascript:
function notify(str) {
window.external.notify(str);
}
El evento capturado usandowv_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);
}
Puse el archivo html enms-appx-web
y funciona bien, y me doy cuenta de que el archivo html debe almacenarse en una carpeta local. Así que cambio elms-appx-web:///.../index.html
ams-appdata:///local/.../index.html
.
Ya busca en el foro de microsoft y consigueesta. En ese hilo hay una solución que usa el resolvedor, pero todavía estoy confuso, ¿cómo puedo notificarlo desde javascript como usarwindow.external.notify
? ¿Y qué tipo de evento en el lado C # que capturará la "notificación" de javascript que no sea "ScriptNotify"?
Hay una solución desdeaquíEjemplo de uso del resolutor y dicho uso.ms-local-stream://
en lugar de usarms-appdata://local
así que todavía puedo usar elScriptNotify
evento. Pero desafortunadamente el ejemplo usando elms-appx
eso significa usar elInstalledLocation
no laLocalFolder
.
Tratando de googlear y buscar enmsdn sitio para la documentación dems-local-stream
Pero la única documentación es solo el formato dems-local-stream
sin ningún ejemplo como estems-local-stream://appname_KEY/folder/file
.
Basado en esa documentación, hice algunos ejemplos para probarlo:
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"); }
}
}
Y dentro de mi 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);
}
Siempre recibe la excepción cuando alcanza elStorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
línea.
Si alguien alguna vez tuvo este problema y ya lo resolvió, por favor avise.