问题
I need to create an HTML page from a C# code with HTML Agility Pack. What I want is that if the element I'm trying to write cannot fit into a div with a specific height, it must create a new div and close the other one (like a paging if you want). My big concern is how to know the height of my element inside C#, I assume I'm force to render it somewhere with the CSS associated, but how?
回答1:
One way to do this is using an HTML renderer like WebKit.NET.
You would then be able to call the javascript in it to retrieve DOM objects properties like heights, position, etc...
Once you have set and rendered the content you want to test (tutorial here), use StringByEvaluatingJavaScriptFromString
function to access javascript functions.
string height = webKitBrowser1.StringByEvaluatingJavaScriptFromString("$('#divID').height().toString()");
Note that you need to return a string value as this function does not return other types.
来源:https://stackoverflow.com/questions/18613721/c-sharp-render-html-to-get-elements-height