HTMLTextBlock for Windows Phone 7

I am trying to include an html text box in my Windows Phone 7. I see code example here. The problem is that the HTMLPage class does not exist on Windows 7, or rather, System.Windows.Browser does not exist. Does anyone know an alternative for this?

+6
html windows-phone-7 silverlight
source share
3 answers

I struggled with this for all of the same reasons and eventually came up with a solution. I need to show a bunch from the inside in a ListBox for my Septic Companion application . Right now, my decision is only bold or italic (like everything I cared about), but it would be easy to change it to deal with more. First, in my ViewModel, I wrote a procedure to return a TextBlock given an HTML string.

private TextBlock MakeFormattedTextBlock(string shtml) { TextBlock tb = new TextBlock(); Run temprun = new Run(); int bold = 0; int italic = 0; do { if ((shtml.StartsWith("<b>")) | (shtml.StartsWith("<i>")) | (shtml.StartsWith("</b>")) | (shtml.StartsWith("</i>"))) { bold += (shtml.StartsWith("<b>") ? 1 : 0); italic += (shtml.StartsWith("<i>") ? 1 : 0); bold -= (shtml.StartsWith("</b>") ? 1 : 0); italic -= (shtml.StartsWith("</i>") ? 1 : 0); shtml = shtml.Remove(0,shtml.IndexOf('>') + 1); if (temprun.Text != null) tb.Inlines.Add(temprun); temprun = new Run(); temprun.FontWeight = ((bold > 0) ? FontWeights.Bold : FontWeights.Normal); temprun.FontStyle = ((italic > 0) ? FontStyles.Italic : FontStyles.Normal); } else // just a piece of plain text { int nextformatthing = shtml.IndexOf('<'); if (nextformatthing < 0) // there isn't any more formatting nextformatthing = shtml.Length; temprun.Text += shtml.Substring(0, nextformatthing); shtml = shtml.Remove(0, nextformatthing); } } while (shtml.Length > 0); // Flush the last buffer if (temprun.Text != null) tb.Inlines.Add(temprun); return tb; } 

Then I just need a way to build this in my XAML. This may not be the best solution, but I first did another procedure to return a StackPanel containing this TextBlock with the text I wanted.

 public StackPanel WordBlock { get { StackPanel sp = new StackPanel(); TextBlock tbWord = MakeFormattedTextBlock("<b>" + Word + "</b>: " + Desc); sp.Children.Add(tbWord); return sp; } } 

To bind this to a visible control, I made a DataTemplate for my ListBox, which just read the entire StackPanel from my view model.

 <DataTemplate x:Key="WordInList2"> <ContentControl Content="{Binding WordBlock}"/> </DataTemplate> 

As I said, there may be parts of it that don't work as elegantly as they can be, but that did what I wanted. Hope this works for you!

+6
source share

Hi, I have converted SilverlightHtmlTextBlock to WP7 here. I have not tested it for terribly complex cases and explosions on dtd tags, but it does the job for simpler html cases and sounds like you were looking for.

+6
source share
+4
source share

All Articles