简体   繁体   English

脚本通知ms-appdata

[英]Script Notify for ms-appdata

I want to notify my web view from button in html file and trigger the javascript: 我想从html文件中的按钮通知我的网页视图并触发javascript:

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

The event captured using wv_ScriptNotify(..., ...) : 使用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 set the html file on ms-appx-web and it running well, and I realize that the html file must be store into local folder. 我在ms-appx-web上设置了html文件并且运行良好,我意识到html文件必须存储到本地文件夹中。 So I change the ms-appx-web:///.../index.html to ms-appdata:///local/.../index.html . 所以我将ms-appx-web:///.../index.htmlms-appdata:///local/.../index.html

Already search in microsoft forum and get this . 已经在微软论坛中搜索并获得此信息 On that thread there is a solution using resolver, but I'm still confusing, how can it notify from javascript like using window.external.notify ? 在该线程上有一个使用解析器的解决方案,但我仍然困惑,它如何通过javascript通知使用window.external.notify And what kind of event in C# side that will capture the "notify" from javascript other than "ScriptNotify"? 在C#方面哪种事件会从“ScriptNotify”以外的javascript中捕获“通知”?


Update 更新

There is a solution from here , example using the resolver and it said to use ms-local-stream:// rather than using ms-appdata://local so I can still use the ScriptNotify event. 有从溶液这里使用的解析器,例如,它说使用ms-local-stream:// ,而不是使用ms-appdata://local ,所以我仍然可以使用ScriptNotify事件。 But unfortunately the example using the ms-appx that means using the InstalledLocation not the LocalFolder . 但不幸的是,使用ms-appx LocalFolder的示例意味着使用InstalledLocation而不是LocalFolder

Trying to googling and search in msdn site for the documentation for ms-local-stream but the only documentation is just the format of ms-local-stream without any example like this ms-local-stream://appname_KEY/folder/file . 尝试谷歌搜索并在msdn网站上搜索ms-local-stream的文档,但唯一的文档只是ms-local-stream的格式,没有任何像这样的ms-local-stream://appname_KEY/folder/file

Based from that documentation, I made some sample to try it: 根据该文档,我做了一些示例来尝试:

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: 在我的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 get the exception when it reach the StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); 当它到达StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);时它总是得到异常StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri); line. 线。

If anybody ever got this problem and already solved it, please advise. 如果有人遇到这个问题已经解决了,请指教。

After debugging it, I found something interesting, the BuildLocalStreamUri part is already make the ms-local-stream automatically. 调试之后,我发现了一些有趣的东西, BuildLocalStreamUri部分已经自动生成了ms-local-stream

I made some changes on the getContent method inside StreamUriWinRTResolver class: 我对StreamUriWinRTResolver类中的getContent方法做了一些更改:

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 on the MainPage.xaml.cs: 更改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...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM