Skrypt Powiadamiaj dla ms-appdata
Chcę powiadomić mój widok internetowy z przycisku w pliku HTML i uruchomić javascript:
function notify(str) {
window.external.notify(str);
}
Zdarzenie zrobione za pomocą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);
}
Ustawiłem plik html nams-appx-web
i działa dobrze, i zdaję sobie sprawę, że plik html musi być przechowywany w folderze lokalnym. Więc zmieniamms-appx-web:///.../index.html
doms-appdata:///local/.../index.html
.
Już szukaj na forum Microsoft i pobierzto. W tym wątku znajduje się rozwiązanie używające resolvera, ale nadal mylę, jak może powiadamiać z javascript jak używającwindow.external.notify
? A jakie zdarzenie po stronie C # przechwytuje „powiadomienie” z javascript innego niż „ScriptNotify”?
Jest rozwiązanie ztutaj, przykład przy użyciu resolvera i mówi się o użyciums-local-stream://
zamiast używaćms-appdata://local
więc mogę nadal korzystać zScriptNotify
zdarzenie. Ale niestety przykład przy użyciums-appx
to oznacza użycieInstalledLocation
nieLocalFolder
.
Próbuję wyszukiwać i wyszukiwaćmsdn strona z dokumentacją dlams-local-stream
ale jedyną dokumentacją jest tylko formatms-local-stream
bez takiego przykładums-local-stream://appname_KEY/folder/file
.
Na podstawie tej dokumentacji zrobiłem próbkę, aby to wypróbować:
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"); }
}
}
Wewnątrz mojego 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);
}
Zawsze dostaje wyjątek, gdy osiągnieStorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
linia.
Jeśli ktokolwiek kiedykolwiek miał ten problem i już go rozwiązał, proszę doradzić.