Script Notify für ms-appdata
Ich möchte meine Webansicht über die Schaltfläche in der HTML-Datei benachrichtigen und das Javascript auslösen:
function notify(str) {
window.external.notify(str);
}
Das Ereignis erfasst mitwv_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);
}
Ich habe die HTML-Datei aktiviertms-appx-web
und es läuft gut, und mir ist klar, dass die HTML-Datei in einem lokalen Ordner gespeichert werden muss. Also ändere ich diems-appx-web:///.../index.html
zums-appdata:///local/.../index.html
.
Schon im Microsoft Forum suchen und bekommendiese. Auf diesem Thread gibt es eine Lösung mit Resolver, aber ich bin immer noch verwirrend, wie kann es aus Javascript wie mit benachrichtigenwindow.external.notify
? Und welche Art von Ereignis auf der C # -Seite erfasst das "Benachrichtigen" von Javascript außer "ScriptNotify"?
Es gibt eine Lösung vonHierBeispiel mit dem Resolver und es sagte zu verwendenms-local-stream://
anstatt zu verwendenms-appdata://local
so kann ich das noch nutzenScriptNotify
Veranstaltung. Aber leider das Beispiel mit demms-appx
das heißt mit demInstalledLocation
nicht derLocalFolder
.
Ich versuche zu googeln und zu suchenmsdn Website für die Dokumentation fürms-local-stream
aber die einzige dokumentation ist nur das format vonms-local-stream
ohne ein solches Beispielms-local-stream://appname_KEY/folder/file
.
Ausgehend von dieser Dokumentation habe ich ein Beispiel erstellt, um es zu versuchen:
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"); }
}
}
Und in meiner 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);
}
Es wird immer die Ausnahme gemacht, wenn es das erreichtStorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
Linie.
Wenn jemand dieses Problem jemals hat und es bereits gelöst hat, teilen Sie es uns bitte mit.