简体   繁体   中英

InvokeScript throws exception the second time through (works first time)

I have been assigned the task of solving a Windows Phone C# problem, but unfortunately I have never done any web development or phone development, so I'm in a bit of trouble. After several hours of beating my head in, I gave up and came here to ask... hope this question isn't too vague, as I'm afraid it could be...

This mobile app is doing a bit of screen scraping, and has this line in it:

wb.InvokeScript ("eval", "document.getElementById('ctl00_ContentPlaceHolder1_textboxFirstName').value = '" + firstName + "';");

It works perfectly the first time through, but the second time through, it crashes. The error message is this:

An exception of type 'System.SystemException' occurred in Microsoft.Phone.Interop.ni.dll but was not handled in user code Additional information: An unknown error has occurred. Error: 80020101.

I am having trouble following this code (I know I'm in over my head). Does anyone know why this line would crash the second time through? The syntax seems to be correct. If I comment out this line, the program crashes on the very next InvokeScript line. Perhaps you just can't Invoke the same line twice?

Any ideas would be much appreciated.

Thank you.

I know this is an old question but I was having similar issues[1] and hopefully my findings might help someone else.

Just as some background, I was attempting to access anchor links within an HTML page that I loaded up to a webview control (WebView.NavigateToString("html")). The anchors could be selected via textblocks (outside of the webview obviously). After some investigation, I came to the conclusion that the webview control didn't like the javascript I was using to navigate to the anchor.

My initial try at a javascript function was simple, but ineffective:

function scrollToAnchor(id) { window.location.hash = id; } 

This would work the first time but never again until I loaded the HTML page again.


I ended up using the below code, simple enough but it took ages to find something that worked consistantly (maybe a more advanced pureJS person would have know this from step one)...

function scrollToAnchor(id) { location.href = id; }

So, in conclusion it looks like these errors can typically be attributed to bad / unsupported JS code. Maybe try the code in IE first and it if works there, it should work in Windows Phone's webview control.

My javascript would work the first time I ran invokescript but any subsequent attempts always failed with the '80020101' exception. [1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM