脚本通知ms-appdata

Cra*_*ezz 6 html javascript c# windows-8.1

我想从html文件中的按钮通知我的网页视图并触发javascript:

function notify(str) {
    window.external.notify(str);
}
Run Code Online (Sandbox Code Playgroud)

使用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);
}
Run Code Online (Sandbox Code Playgroud)

我设置了html文件ms-appx-web并且运行良好,我意识到html文件必须存储到本地文件夹中.所以我改变ms-appx-web:///.../index.htmlms-appdata:///local/.../index.html.

已经在微软论坛中搜索并获得此信息.在那个线程上有一个解决方案使用解析器,但我仍然困惑,它如何通过javascript通知使用window.external.notify?在C#方面哪种事件会从"ScriptNotify"以外的javascript中捕获"通知"?


更新

有从溶液这里使用的解析器,例如,它说使用ms-local-stream://,而不是使用ms-appdata://local,所以我仍然可以使用ScriptNotify事件.但不幸的是ms-appx,使用的例子意味着使用InstalledLocation不是LocalFolder.

尝试谷歌搜索和搜索msdn网站的文档,ms-local-stream但唯一的文档只是ms-local-stream没有这样的例子的格式ms-local-stream://appname_KEY/folder/file.

根据该文档,我做了一些示例来尝试:

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"); }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的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);
}
Run Code Online (Sandbox Code Playgroud)

它到达StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);生产线时总是会遇到异常.

如果有人遇到这个问题并且已经解决了,请指教.

Cra*_*ezz 7

经过调试后,我发现了一些有趣的东西,BuildLocalStreamUri部分已经ms-local-stream自动生成了.

我对类中的getContent方法做了一些更改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"); }
    }
}
Run Code Online (Sandbox Code Playgroud)

更改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...
    }
}
Run Code Online (Sandbox Code Playgroud)