Javascript 在Webview控件中修改传输的数据
我想将内容注入名为from WebView Control的网页,html代码为:Javascript 在Webview控件中修改传输的数据,javascript,c#,xaml,webview,uwp,Javascript,C#,Xaml,Webview,Uwp,我想将内容注入名为from WebView Control的网页,html代码为: <section id="header" class="header-section section-padding"> <div class="container"> <div class="row"> <div class="text-center"> <h2 class="s
<section id="header" class="header-section section-padding">
<div class="container">
<div class="row">
<div class="text-center">
<h2 class="section-title">Text I Want to Change</h2>
</div>
</div>
</div>
</section>
但这会产生错误。我不擅长javascript,那么如何更改h2
标记中的文本
Xaml代码:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="WebView"
LoadCompleted="WebView_LoadCompleted"
Source="http://info.vit.ac.in/gravitas2015/tg15/index.html" />
<ProgressRing x:Name="ProgressRing"
Width="100"
Height="100"
Foreground="BlueViolet"
IsActive="True" />
</Grid>
我得到的错误:
System.Exception was unhandled by user code
HResult=-2147352319
Message=Exception from HRESULT: 0x80020101
Source=mscorlib
StackTrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WindowsApp2.Views.Organisers.<WebView_LoadCompleted>d__1.MoveNext()
InnerException:
System.Exception未由用户代码处理
HResult=-2147352319
消息=来自HRESULT的异常:0x80020101
Source=mscorlib
堆栈跟踪:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
在WindowsApp2.Views.Organizers.d_u1.MoveNext()上
内部异常:
复制该问题:
提前谢谢 这里的问题是,在html页面中没有
节的id
是“header”:
如果我们在web浏览器(例如Edge)中测试JavaScript代码,我们会发现它会抛出如下错误:
这就是为什么在代码隐藏中调用InvokeScriptAsync
时出现异常的原因。我不确定您想更改哪个部分,但是如果您像下面这样编辑JavaScript字符串,您的代码应该可以工作
string functionString = String.Format("document.getElementById('portfolio').getElementsByTagName('h2')[0].innerHTML = 'new text';");
除此之外,在Windows 8.1之后的版本中可能会被更改或不可用。相反,请使用。“但这会产生错误。”-您需要包括准确的错误消息,以及预期的行为和观察到的行为。我无法重现您的问题。你的代码是对的。如何将页面加载到“WebView”中?何时调用InvokeScriptAsync
方法?你能分享一个能重现你的问题的例子吗?
System.Exception was unhandled by user code
HResult=-2147352319
Message=Exception from HRESULT: 0x80020101
Source=mscorlib
StackTrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at WindowsApp2.Views.Organisers.<WebView_LoadCompleted>d__1.MoveNext()
InnerException:
string functionString = String.Format("document.getElementById('portfolio').getElementsByTagName('h2')[0].innerHTML = 'new text';");