As you know, the webbrowser control is very powerful in WP7 development. In basic applications, it is a good choice to display long texts, especially the content captured on webpages. In advanced applications, you can use HTML, CSS, and js to create cool effects. There are many good articles about webbrowser in the garden, which are worth learning. Here are two tips for webbrowser development. They are basic applications of the webbrowser control and hope to help you.
1. Use webbrowser
XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <phone:WebBrowser Name="webBrowser" Margin="12,0,0,0"/></Grid>
Code
Public partial class mainpage: phoneapplicationpage {// constructor public mainpage () {initializecomponent (); this. loaded + = new routedeventhandler (mainpage_loaded);} private void mainpage_loaded (Object sender, routedeventargs e) {string content = @ "If you shed tears when you miss the sun, you also miss the stars. "; string rawhtml =" "; rawhtml + =" <HTML> <body> "; rawhtml + = content; rawhtml + =" </body> <HTML> "; webbrowser. navigatetostring (rawhtml );}}
The background color of the webbrowser control is white by default and cannot be modified in the control properties. If the overall background color of the application is black and the webbrowser control is not harmonious, how can I modify the display?
2. Modify the background color of the webbrowser Control
As you know, the webbrowser control is actually a browser page, and the display of this page is undoubtedly controlled by HTML. As you may already know, you can use the bgcolor attribute to control the background color.
Code:
private void MainPage_Loaded(object sender, RoutedEventArgs e) { string content = @"If you shed tears when you miss the sun, you also miss the stars."; string rawHtml = ""; if (IsThemeDark()) { rawHtml += " }
Use isthemedark to determine whether the default background color of the current application is black, so that the background color of the webbrowser control is consistent with the Default background color of the app. In this case, the app uses the default black background to run the app and finds a piece of black, so the testeor is missing. What is going on? It turns out that it is difficult to change the webbrowser watermark. The font color is still black. Fortunately, we still have a little understanding of HTML and continue:
private void MainPage_Loaded(object sender, RoutedEventArgs e) { string content = @"If you shed tears when you miss the sun, you also miss the stars."; string rawHtml = ""; if (IsThemeDark()) { rawHtml += "
The code snippet changes the text color to the background color of the webbrowser control.
If you run the above Code together with the author, you will find that the webbrowser control blinks during the loading process, and the white background is flashing. It seems that Jiangshan is easy to change, and the Nature is hard to move. How can we prevent the webbrowser control from exposing white teeth?
3. Prevent flickering during webbrowser Control Loading
Analyze the causes of flickering. The background color of the webbrowser control is white. After loading our HTML, the background is changed to black. After the webbrowser control is displayed and HTML is loaded, the webbrowser control is white. This was a short time, but it was captured by our eye. The reason is clear. Let's consider the solution: What we want is to load the webbrowser after HTML is completed. What is it like before? We don't care, since we don't care, simply hide it, and you can guess it all the way back.
XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <phone:WebBrowser Name="webBrowser" Margin="12,0,0,0" Opacity="0" LoadCompleted="WebBrowser_LoadCompleted"/></Grid>
Set the opacity property of the webbrowser control to 0 and hide it by default. Process the loadcompleted event.
Code
private void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e) { webBrowser.Opacity = 1; }
In response to the loadcompleted event, the webbrowser control is displayed only after it is loaded. Run, the effect is good, the whole world is quiet.
Iv. Summary
Set the background color and prevent flickering. This is only a basic problem encountered in the webbrowser application, such as garbled characters when loading Chinese characters. You can search for other articles in the garden. As we have already said, webbrowser is very powerful. In particular, Mango has added support for HTML5. developers can use HTML5 technology to create more personalized native applications based on webbrowser. More importantly, HTML5-based development is cross-platform. Many open-source projects support WP7, such as jquery. Mobile and phonegap. If you are free, try again.
Code download
(Full text)