Suppose
now that the user enters "Ford" in the text box of the photo browsing
application developed in the previous section. Unless you query the
user, you won't know whether the user meant "Ford Mustang" or "Ford
F-150." One way to find out would be to create a page with HTML markup
and display it to the user, asking for more information. You could then
save the generated file to Isolated Storage, and load it using the
technique described in the previous section (Using a WebBrowser Control
to Display Local HTML Content). But that would certainly be a cumbersome
approach for such a simple task. Luckily, there's a much easier way to
show a dynamically generated HTML page: using the NavigateToString()
method of the WebBrowser control. This method takes a single argument—a
string—that contains all of the HTML needed to display the page you
have in mind in the WebBrowser control.
The next walkthrough shows just how easy it is to use this method.
Bring up the MainPage.xaml.cs file in the code editor (either by double-clicking it or clicking the MainPage.xaml file in the Solution Explorer and choosing View Code).
Next, you will construct the HTML code to display to the user. Make the button1_Click method look identical to the following code—notice how NavigateToStringloads
up what amounts to a basic HTML page directly into the WebBrowser
control, without your having to save this HTML to the Isolated Storage.
Also note that building an HTML string in code becomes a bit ugly very
quickly, so NavigateToStringshould not be abused for large HTML messages/files.
webBrowser1.NavigateToString(@"<html>
<body><center><div style='font: Arial 12px;'>
Which Ford
model would you like to see?<br><br>
<a href='http://www.bing.com/images/search?q=cars+Ford+Mustang'>Ford
Mustang</a> or <a href='http://www.bing.com/images/search?q=cars+Ford+F150'>Ford F-
150</a></div></center></body></html>");
Press
F5 to run the application. Now if you type "Ford" in the text box and
press the "Show It!" button, you should see a dynamically generated HTML
message with hyperlinks asking you to clarify which Ford model you
would like to see, just like Figure 1.
Many web-based
applications have been built, from translators to elaborate e-commerce
systems, and all of them are easily accessible and could even be
potentially enhanced with the use of the WebBrowser control on Windows
Phone 7. But the WebBrowser control can do even more: in the next
section, we will learn how to save the web pages locally, so that we can
potentially parse certain information or search within them.