这是我在尝试完成一项更大的任务时提出的一系列问题的一部分 - 在 Windows 应用商店应用程序中将 HTML 转换为 RTF。
我很高兴地报告上述内容can做完了。我终于弄清楚如何做到这一点,使用DataPackage
- 通常用于在应用程序之间共享内容。
首先,这个 javascript 函数必须存在于 webview 中加载的 HTML 中。
function select_body() {
var range = document.body.createTextRange();
range.select();
}
接下来,您需要添加using Windows.ApplicationModel.DataTransfer;
到文档的顶部。没有足够的 StackOverflow 答案提及所使用的命名空间。我总是得去寻找他们。
这是发挥神奇作用的代码:
// call the select_body function to select the body of our document
MyWebView.InvokeScript("select_body", null);
// capture a DataPackage object
DataPackage p = await MyWebView.CaptureSelectedContentToDataPackageAsync();
// extract the RTF content from the DataPackage
string RTF = await p.GetView().GetRtfAsync();
// SetText of the RichEditBox to our RTF string
MyRichEditBox.Document.SetText(Windows.UI.Text.TextSetOptions.FormatRtf, RTF);
我花了大约两周的时间试图让它发挥作用。最终发现我不必手动将文件编码为 RTF,这让我松了一口气。现在,如果我能让它以相反的方式工作,我会欣喜若狂。对于我正在构建的应用程序来说不是必需的,但这将是一个可爱的功能。
UPDATE
回想起来,您可能不需要在 HTML 中包含该函数,您可能可以摆脱这个(尽管我还没有测试过):
MyWebView.InvokeScript("execScript", new string[] {"document.body.createTextRange().select();"})