适用于Windows Phone 7的HTMLTextBlock

适用于Windows Phone 7的HTMLTextBlock,html,silverlight,windows-phone-7,Html,Silverlight,Windows Phone 7,我正试图在我的WindowsPhone7中包含一个html文本框。我看到一些。问题是HTMLPage类在windows phone 7中不存在,或者更确切地说,System.windows.Browser不存在。有人知道这种方法的替代方法吗?WebBrowser可以呈现html 出于同样的原因,我一直在努力解决这个问题,最终找到了一个解决方案。我需要在一个列表框中为我的客户展示一堆这些。现在,我的解决方案只处理粗体或斜体(这就是我所关心的),但修改它以处理更多内容将很容易。首先,在我的ViewM

我正试图在我的WindowsPhone7中包含一个html文本框。我看到一些。问题是HTMLPage类在windows phone 7中不存在,或者更确切地说,System.windows.Browser不存在。有人知道这种方法的替代方法吗?

WebBrowser可以呈现html


出于同样的原因,我一直在努力解决这个问题,最终找到了一个解决方案。我需要在一个列表框中为我的客户展示一堆这些。现在,我的解决方案只处理粗体或斜体(这就是我所关心的),但修改它以处理更多内容将很容易。首先,在我的ViewModel中,我编写了一个例程来返回给定HTML字符串的TextBlock

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;
}
private TextBlock MakeFormattedTextBlock(字符串shtml)
{
TextBlock tb=新的TextBlock();
Run temprun=新运行();
int bold=0;
int斜体=0;
做
{
if((shtml.StartsWith(“”))|(shtml.StartsWith(“”)|
(shtml.StartsWith(“”)|(shtml.StartsWith(“”))
{
粗体+=(shtml.StartsWith(“”)1:0);
斜体+=(斜体开始,以(“”)1:0表示);
粗体-=(shtml.StartsWith(“”)1:0);
斜体-=(以(“”?1:0)开头);
shtml=shtml.Remove(0,shtml.IndexOf('>')+1);
if(temprun.Text!=null)
tb.Inlines.Add(temprun);
temprun=新运行();
temprun.FontWeight=((粗体>0)?FontWeights.bold:FontWeights.Normal);
temprun.FontStyle=((斜体>0)?FontStyles.italic:FontStyles.Normal);
}
否则//只是一段纯文本
{
int-nextformatthing=shtml.IndexOf('0);
//刷新最后一个缓冲区
if(temprun.Text!=null)
tb.Inlines.Add(temprun);
返回结核病;
}
然后我只需要一种方法将其构建到我的XAML中。这可能不是最好的解决方案,但我首先制作了另一个例程,返回一个包含我想要的文本块的StackPanel

public StackPanel WordBlock
{
    get
    {
        StackPanel sp = new StackPanel();
        TextBlock tbWord = MakeFormattedTextBlock("<b>" + Word + "</b>: " + Desc);
        sp.Children.Add(tbWord);
        return sp;
    }
}
公共堆栈面板字块
{
得到
{
StackPanel sp=新的StackPanel();
TextBlock tbWord=MakeFormattedTextBlock(“+Word+”:“+Desc”);
sp.Children.Add(待定);
返回sp;
}
}
为了将其绑定到可见控件,我为我的ListBox创建了一个DataTemplate,它只需从我的视图模型中读取整个StackPanel

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

正如我所说,这其中可能有一部分做得不够优雅,但这是我想要的。希望它对你有用!

嘿,我把它转换成了WP7 I
我还没有在非常复杂的情况下测试过它,也没有在dtd标签上测试过它,但是,它可以在更简单的html情况下使用,而且听起来就像你在寻找的一样。

我确实知道,但我不想使用WebBrowser,原因有很多:-UI更难更改,事件手势也更难处理HTMLTextBlock@vodkhang当前位置没有这样的事情“HTMLTextBlock”使用WebBrowser控件是当前在WP7应用程序中显示html内容的唯一方法。嗯,事实上,我想到了类似的事情。您试图实现什么?为什么需要在页面中包含html而不在WebBrowser控件中?因为WebBrowser将捕获所有水平滚动事件,这是我最关心的。我知道有一种叫做HTMLTextBlock的东西,用于web,如下所示: