gpt4 book ai didi

c# - 在 webview 商店应用程序中调用 Javascript

转载 作者:行者123 更新时间:2023-11-28 08:56:12 26 4
gpt4 key购买 nike

我正在使用 C# 和 XAML 开发 Windows 商店应用程序,在我的 web View 中我正在加载本地 HTML 文件,之后我尝试使用 JavaScript 来水平滚动 HTML。

在此之前,我尝试使 HTML 高度适合 Windows 高度。对我来说没什么事,

我的代码:

    protected async override void OnNavigatedTo(NavigationEventArgs e)
{
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync("split.html");
string Filecontent = await FileIO.ReadTextAsync(file);
webView.NavigateToString(Filecontent);
}
private async void webView_LoadCompleted(object sender, NavigationEventArgs e)
{
StorageFolder folder = Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync("JavaScript.js");
string js = await FileIO.ReadTextAsync(file);
webVIew.InvokeScript("eval", new[] { js });
}

JavaScript 代码:

var h = window.innerHeight, w = window.innerWidth; window.resizeBy(w, h);

我不认为这是一个更好的解决方案,但我想知道这一点,如果有人对此有任何建议,它是有用的。谢谢。

最佳答案

我已经完成了这个工作,并且我正在做两件事:

首先,将 javascript 包含在 HTML 中,可能包含在 head 标记中:

<script type="text/javascript" src="JavaScript.js"/>

其次,您不需要调用“eval”。您可以直接调用 JavaScript 函数,如果您愿意,还可以包含多个参数:

string result = webview->InvokeScript("myScriptName", new string[]{ parameterString });
string result = webview->InvokeScript("scriptTwo", new string[]{ param1, param2 });

关于c# - 在 webview 商店应用程序中调用 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18462964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com