Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTMLTextBlock for Windows Phone 7

I am trying to include a html textbox into my windows phone 7. I see some sample code here. The problem is that the HTMLPage class doesn't exist in windows phone 7, or more exactly, the System.Windows.Browser does not exist. Do anybody know an alternative for this?

like image 251
vodkhang Avatar asked Dec 22 '10 02:12

vodkhang


2 Answers

I struggled with this for all the same reasons, and eventually came up with a solution. I need to show a bunch of these inside a ListBox for my Septic's Companion app. Right now my solution only deals with bold or italic (as that's all I cared about) but it would be easy to modify it to deal with more. First, into my ViewModel I wrote a routine 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 needed a way to build this into my XAML. This may not be the very best solution, but I first made another routine to return a StackPanel containing that 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 then made a DataTemplate for my ListBox which simply read the entire StackPanel out of my view model.

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

As I say, there may be parts of this that aren't done as elegantly as they might be, but this did what I wanted. Hope it works for you!

like image 99
Chris Rae Avatar answered Nov 08 '22 23:11

Chris Rae


Hey I converted the SilverlightHtmlTextBlock to WP7 Here. I haven't tested it for terribly complex cases and blows up on dtd tags but, it does the job for simpler html cases and sounds like what you were looking for.

like image 22
Terrance Avatar answered Nov 09 '22 01:11

Terrance