winrt:在Webview中加载本地html及其资源 [英] winrt: load local html and its resources in webview

查看:213
本文介绍了winrt:在Webview中加载本地html及其资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发WinRT应用程序,在其中我可以从Windows.Storage.ApplicationData.Current.LocalFolder加载本地HTML文件.我想知道什么是加载嵌入在HTML文件中的资源(如图像,音频,视频,CSS,JavaScript等)的最佳方法.

I'm currently developing a WinRT app where I load local HTML files from Windows.Storage.ApplicationData.Current.LocalFolder. I like to know what would be the best way to load assets like Images, Audio, Video, CSS, JavaScripts, etc, that are embedded in the HTML file.

当前,我正在将这些文件写入Windows.ApplicationModel.Package.Current.InstalledLocation中,但是一旦从商店中部署了应用程序或通过Windows PowerShell安装了AppPackage,将无法再访问这些文件,除非您更改程序文件"的所有权/WindowsApps"文件夹添加到当前用户或所有人".

Currently, I'm writing these files in the Windows.ApplicationModel.Package.Current.InstalledLocation but this wont be accessible anymore once the application had been deployed from the store or by installing the AppPackage through Windows PowerShell, unless you change the ownership of the "Program Files/WindowsApps" folder to the current user or to "Everyone".

谢谢.

推荐答案

我不确定您在这里问的是什么.唯一应为Windows.ApplicationModel.Package.Current.InstalledLocation的文件是与AppPackage一起安装的文件,即是您的应用程序分发的一部分.您的应用程序不应(甚至不允许)将任何文件存储在那里.

I'm not sure what exactly you're asking here. The only files that should be Windows.ApplicationModel.Package.Current.InstalledLocation are the ones that are installed with your AppPackage, i.e. are a part of your app distribution. Your app shouldn't (and is even not allowed) to store any of its files there.

在运行时生成的任何文件都应存储在Windows.Storage.ApplicationData.Current.LocalFolderWindows.Storage.ApplicationData.Current.RemoteFolderWindows.Storage.ApplicationData.Current.TempFolder之一中,具体取决于您希望如何保存它们.该规则的唯一例外是您希望用户甚至从应用程序外部访问这些文件的情况-然后您应将它们放在库中(并要求对其进行访问)或使用文件选择器向用户询问位置

Any files that are generated at runtime should be stored in one of Windows.Storage.ApplicationData.Current.LocalFolder, Windows.Storage.ApplicationData.Current.RemoteFolder and Windows.Storage.ApplicationData.Current.TempFolder, depending on how you want them to be preserved. The only exception to this rule is the case when you want the user to access these files even from outside your app - then you should put them in a library (and require access to it) or ask the user for a location using a file picker.

加载本地HTML资源非常有限,除非它们与应用程序一起分发,即它们存储在InstalledLocation中并通过ms-appx-web协议进行访问.如文档所述,控件不支持用于从LocalStorageRemoteStorageTempFolder检索数据的ms-appdata协议:

Loading local HTML resources is very much limited unless they are distributed along with the application, i.e. they are stored in InstalledLocation and accessed via ms-appx-web protocol. As documented, WebView control doesn't support ms-appdata protocol which is used to retrieve data from LocalStorage, RemoteStorage or TempFolder:

此外,尽管WebView不支持ms-appdata:方案,但是 它确实支持ms-appx-web:方案.这使您可以加载 项目中的内容文件.

Furthermore, WebView does not support the ms-appdata: scheme, although it does support the ms-appx-web: scheme. This enables you to load content files in your project.

这或多或少消除了C#项目中的所有选项.您可以使用其他HTML查看器控件,但我知道只有一个,它不是还没有完成.

This more or less takes away all your options in C# projects. You could use an alternative HTML viewer control, but I know of only one and it isn't even finished yet.

在JavaScript中,情况只会稍微好一点. ms-appdata协议是仅支持数据文件:

In JavaScript the situation is only slightly better. ms-appdata protocol is supported only for data files:

有时,引用从中下载的资源很有用. 互联网访问您应用的本地ApplicationData存储(通过Windows 运行时API).要引用此类内容,您必须使用方案 "ms-appdata:",以及ApplicationData中文件的路径 本地存储.请注意,出于安全原因,您无法导航到 您已下载到此位置的HTML,并且无法运行任何HTML 可执行文件或潜在的可执行代码,例如脚本或CSS.它 适用于图像或视频等媒体.

Sometimes it is useful to refer to resources you have downloaded from the Internet to your app’s local ApplicationData storage (via Windows Runtime APIs). To refer to such content, you must use the scheme "ms-appdata:", with the path to the file within your ApplicationData local storage. Note that, for security reasons, you cannot navigate to HTML you have downloaded to this location and you cannot run any executable or potentially executable code, such as script or CSS. It is intended for media such as images or videos and the like.

这意味着您可以执行以下操作:

This means you can do:

<img src="ms-appdata:///local/img/coal.jpg" />

但不是:

<iframe src="ms-appdata:///local/index.html" />

ms-appdata原始语法为有据可查万一您仍然可以找到它的用途.

ms-appdata protocal syntax is well documented in case you will still find use for it.

这篇关于winrt:在Webview中加载本地html及其资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆