Javascript 如何在UWPC上从appdata中的html接收scriptNotify#
如果html是从ms appdata加载的,Windows将不允许my WebView_ScriptNotify事件接收调用 我知道我可以使用ms appx web协议从我的应用程序包中加载这样的文件,但这不是选项,因为要显示的数据是在安装应用程序后下载的。 我也不能只使用webView.navigateToString,因为它不会在html文件中包含引用的库 目前我正在Class.xaml.cs中尝试类似的方法Javascript 如何在UWPC上从appdata中的html接收scriptNotify#,javascript,c#,html,windows-10,uwp,Javascript,C#,Html,Windows 10,Uwp,如果html是从ms appdata加载的,Windows将不允许my WebView_ScriptNotify事件接收调用 我知道我可以使用ms appx web协议从我的应用程序包中加载这样的文件,但这不是选项,因为要显示的数据是在安装应用程序后下载的。 我也不能只使用webView.navigateToString,因为它不会在html文件中包含引用的库 目前我正在Class.xaml.cs中尝试类似的方法 WebView webView = new WebView(); webVie
WebView webView = new WebView();
webView.ScriptNotify += WebView_ScriptNotify;
Uri navigationUri = new Uri(@"ms-appdata:///local/index.html");
webView.Navigate(navigationUri);
及
在html文件中是
<div id="content">
<div class="btn" onClick="window.external.notify('hello world');"</div>
</div>
参考:
要使内容能够发送通知,以下条件适用:
- 页面的来源应通过NavigateToString()、NavigateToStream()或ms appx web://从本地系统获取
或
- 页面的来源通过https://传递,站点域名列在包清单的应用程序内容URI部分
因此,为了解决这个问题,我们可以在协议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"); }
}
}
公共密封部分类主页面:第页
{
公共主页()
{
this.InitializeComponent();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
//ms本地流协议的URI的“主机”部分需要是包名称的组合
//以及一个应用程序定义的键,用于标识特定的解析器,在本例中为“MyTag”。
Uri url=webView.BuildLocalStreamUri(“MyTag”、“index.html”);
StreamUriWinRTResolver myResolver=新StreamUriWinRTResolver();
//将解析器对象传递给导航调用。
NavigateToLocalStreamUri(url,myResolver);
}
私有void webView_ScriptNotify(对象发送方,NotifyEventArgs e)
{
System.Diagnostics.Debug.WriteLine(“ScriptNotifyValue:+e.Value”);
}
}
公共密封类StreamUriWinRTResolver:IUriToStreamResolver
{
公共IAsyncOperation UriToStreamAsync(Uri)
{
if(uri==null)
{
抛出新异常();
}
字符串路径=uri.AbsolutePath;
//由于该方法的签名,它不能使用wait,所以我们
//调用一个单独的helper方法,该方法可以使用C#wait模式。
返回GetContent(path).asAsAsAsyncOperation();
}
专用异步任务GetContent(字符串路径)
{
//我们使用应用程序的本地文件夹作为源
尝试
{
Uri localUri=新Uri(“ms-appdata:///local“+路径);
StorageFile f=等待StorageFile.GetFileFromApplicationUriAsync(localUri);
irandomaccesstream=wait f.OpenAsync(FileAccessMode.Read);
回流;
}
catch(异常){抛出新异常(“无效路径”);}
}
}
有关更多信息,请参见中的备注和示例,以及中的自定义URI解析。此外,GitHub上还有一个
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"); }
}
}