C# ms appdata的脚本通知

C# ms appdata的脚本通知,c#,javascript,html,windows-8.1,C#,Javascript,Html,Windows 8.1,我想从html文件中的按钮通知我的web视图,并触发javascript: function notify(str) { window.external.notify(str); } 使用wv\u ScriptNotify(…,…)捕获的事件: 我在msappxweb上设置了html文件,它运行良好,我意识到html文件必须存储到本地文件夹中。所以我改变了ms-appx-web:///.../index.html至ms-appdata:///local/.../index.html 已

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

function notify(str) {
    window.external.notify(str);
}
使用
wv\u ScriptNotify(…,…)
捕获的事件:

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

已在microsoft论坛中搜索并获取。在这个线程上有一个使用解析器的解决方案,但我仍然很困惑,它如何能够像使用
window.external.notify那样从javascript发出通知?在C#side中,除了“ScriptNotify”之外,还有什么样的事件会从javascript捕获“notify”


更新 例如,有一个解决方案使用解析器,它说使用
ms local stream://
而不是使用
ms-appdata://local
因此我仍然可以使用
ScriptNotify
事件。但不幸的是,使用
ms-appx
的示例意味着使用
InstalledLocation
而不是
LocalFolder

试图在网站上搜索
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"); }
    }
}
当它到达
StorageFile f=await-StorageFile.getfilefromsapplicationuriasync(localUri)时,总是会得到异常


如果有人遇到这个问题并已经解决了,请告诉我。

调试后,我发现了一些有趣的事情,
BuildLocalStreamUri
部分已经自动生成
ms localstream

我对
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"); }
    }
}
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);
}
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"); }
    }
}
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...
    }
}