wp7 how do i hide or remove header div for web pag

2019-08-07 00:25发布

问题:

I am developing simple app and i want lode and display the web page on image tap but i don't want to display header div content that contains menu, logo image.

for that am able to call and display the web page using web browser control on

my C# code id

private void image_Tap(object sender, GestureEventArgs e)
    {
        webBrowser1.Navigate(new Uri("http://www.my-url.com", UriKind.Absolute));
    }

that works fine.

and now how can i remove header tag data(content in b/w < header >< /header > divs).

thanks in advance

回答1:

Web browser control simply loads the HTML document you pass to it. There is no direct way to manipulate this HTML if the document is not hosted on your own servers. A very raw approach would be to parse the HTML document and then do a Search/Replace on <header>...</header> with <!--<header>..</header>-->
You can use WebBrowser.SaveToString method to save the HTML document source as a string. For HTML parsing in C#, I would suggest HTML Agility Pack. Also have a look at What is the best way to parse html in C#? and Parsing HTML String.



回答2:

If you want to do it with JavaScript:

document.getElementsByTagName('header')[0].style.display = 'none';



回答3:

The WebBrwoserControl in Windows Phone renders the html it gets from the URL.

You can once the HTML is rendered execute javascript code (from your c# code) that manipulates the rendered DOM/HTML - in your case remove an HTML tag. All you have to do is use InvokeScript.

Lets say you want to execute some code when pressing on a button in your Windows Phone app. The Event handler for that button click event is Button1_Click and the code to execute your code is below.

private void Button1_Click(object sender, RoutedEventArgs e)
{
    object results = WB1.InvokeScript("HideElement", new string[] { "hideElement" });
}

All you need to do now is add a function called 'HideElement' to your page, that you load from www.my-url.com and you are set. As the InvokeScript calls that function, when the button is clicked. Just to be clear: that javscript-function needs to be on the web server where your web page that you load is coming from.