javascript - 如何从 UWP c# 上的 appdata 中的 html 接收 scriptNotify

标签 javascript c# html windows-10 uwp

如果 html 从 ms-appdata 加载,Windows 不会让我的 WebView_ScriptNotify 事件接收调用。

我知道我可以使用 ms-appx-web 协议(protocol)从我的应用程序包中加载这样的文件,但这不是选项,因为要显示的数据是在安装应用程序后下载的。 我也不能只使用 webView.navigateToString,因为这不会在 html 文件中包含引用的库。

目前我正在我的 Class.xaml.cs 中尝试这样的事情

 WebView webView = new WebView();
 webView.ScriptNotify += WebView_ScriptNotify;
 Uri navigationUri = new Uri(@"ms-appdata:///local/index.html");
 webView.Navigate(navigationUri);

 private void WebView_ScriptNotify(object sender, NotifyEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("ScriptNotifyValue: " + e.Value);
        //I want to do the magic here, but this will never be called
    }

在html文件中是

<div id="content">
     <div class="btn" onClick="window.external.notify('hello world');"</div>
</div>

此外,无法使用 InvokeScript(),因为我不知道何时必须触发事件及其值。

但必须使用来自 ms-appdata 的文件。

你知道解决这个问题的方法吗? 即使是另一种解决方法也会让我感到惊讶。

最佳答案

引用 Script notify changes in XAML :

For content to be able to send notifications the following conditions apply:

  • The source of the page should be from the local system via NavigateToString(), NavigateToStream() or ms-appx-web:///

Or

  • The source of the page is delivered via https:// and the site domain name is listed in the app content URI’s section of the package manifest.

所以要解决这个问题,我们可以使用 WebView.NavigateToLocalStreamUri method 使用协议(protocol) ms-local-stream://,而不是 ms-appdata://。例如:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    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 = webView.BuildLocalStreamUri("MyTag", "index.html");
        StreamUriWinRTResolver myResolver = new StreamUriWinRTResolver();

        // Pass the resolver object to the navigate call.
        webView.NavigateToLocalStreamUri(url, myResolver);
    }

    private void webView_ScriptNotify(object sender, NotifyEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("ScriptNotifyValue: " + e.Value);
    }
}

public sealed class StreamUriWinRTResolver : IUriToStreamResolver
{
    public IAsyncOperation<IInputStream> UriToStreamAsync(Uri uri)
    {
        if (uri == null)
        {
            throw new Exception();
        }
        string path = uri.AbsolutePath;

        // Because of the signature of the this method, it can't use await, so we
        // call into a seperate helper method that can use the C# await pattern.
        return GetContent(path).AsAsyncOperation();
    }

    private async Task<IInputStream> GetContent(string path)
    {
        // We use app's local folder as the source
        try
        {
            Uri localUri = new Uri("ms-appdata:///local" + path);
            StorageFile f = await StorageFile.GetFileFromApplicationUriAsync(localUri);
            IRandomAccessStream stream = await f.OpenAsync(FileAccessMode.Read);
            return stream;
        }
        catch (Exception) { throw new Exception("Invalid path"); }
    }
}

更多信息,请参见WebView.NavigateToLocalStreamUri method中的备注示例以及 What’s new in WebView in Windows 8.1 中的自定义 URI 解析 .此外,还有一个WebView control (XAML) sample在 GitHub 上。

关于javascript - 如何从 UWP c# 上的 appdata 中的 html 接收 scriptNotify,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39041215/

相关文章:

javascript - Kendo UI TreeView 可选根

javascript - 如何让 iframe 像电影演职员表一样向下滚动?

c# - 如何在 Visual Studio 中获取参数提示/完成?

javascript - JS if else 语句和计时器

html - CSS:尽管缺少边距或填充,但 Div 元素过度滚动

javascript - 在没有服务器交互的情况下在javascript中捕获按钮点击

javascript - 使用 PassportJS、SequelizeJS 和 JWT token 时,req.user 未定义

c# - 在 Windows 资源管理器中将文件夹固定到导航 Pane

c# - 以编程方式双击系统托盘图标 Windows xp/7

php - 无法在返回网址中获取 paypal post pramenters