Windows Phone 7的HTMLTextBlock [英] HTMLTextBlock for Windows Phone 7

查看:75
本文介绍了Windows Phone 7的HTMLTextBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows Phone 7中包含一个html文本框.我看到一些

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?

推荐答案

出于所有相同的原因,我为此苦苦挣扎,并最终提出了解决方案.我需要在 Septic的Companion应用程序的列表框中显示一堆.现在,我的解决方案只处理粗体或斜体(这就是我所关心的全部),但是修改它以处理更多内容很容易.首先,我在ViewModel中编写了一个例程,该例程返回给定HTML字符串的TextBlock.

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;
}

然后,我只需要一种将其构建到XAML中的方法.这可能不是最好的解决方案,但是我首先创建了另一个例程,以返回包含带有所需文本的TextBlock的StackPanel.

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;
    }
}

要将其绑定到可见控件,然后为ListBox制作了一个DataTemplate,它仅从视图模型中读取了整个StackPanel.

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!

这篇关于Windows Phone 7的HTMLTextBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆