适用于Windows Phone 7的HTMLTextBlock
发布时间:2020-12-14 04:36:23 所属栏目:Windows 来源:网络整理
导读:我试图在我的 Windows Phone 7中包含一个html文本框.我看到一些 sample code here.问题是Windows Phone 7中不存在HTMLPage类,或者更确切地说,System.Windows.Browser不存在.有人知道替代方案吗? 出于同样的原因,我在努力解决这个问题,并最终想出了一个解决
我试图在我的
Windows Phone 7中包含一个html文本框.我看到一些
sample code here.问题是Windows Phone 7中不存在HTMLPage类,或者更确切地说,System.Windows.Browser不存在.有人知道替代方案吗?
出于同样的原因,我在努力解决这个问题,并最终想出了一个解决方案.我需要在我的
Septic’s Companion app的ListBox中显示一堆这些.现在我的解决方案只处理粗体或斜体(因为这是我所关心的)但是很容易修改它来处理更多.首先,在我的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; } 然后我只需要一种方法将其构建到我的XAML中.这可能不是最好的解决方案,但是我首先做了另一个例程来返回一个包含TextBlock的StackPanel和我想要的文本. public StackPanel WordBlock { get { StackPanel sp = new StackPanel(); TextBlock tbWord = MakeFormattedTextBlock("<b>" + Word + "</b>: " + Desc); sp.Children.Add(tbWord); return sp; } } 为了将它绑定到一个可见的控件,我然后为我的ListBox创建了一个DataTemplate,它只是从我的视图模型中读取整个StackPanel. <DataTemplate x:Key="WordInList2"> <ContentControl Content="{Binding WordBlock}"/> </DataTemplate> 正如我所说的,可能有一些部分并没有像它们那样优雅地完成,但这样做了我想要的.希望这对你有用! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Windows 7中的断电事件… C#问题
- windows-7 – 如何在Windows 7上设置LDAP服务器?
- windows – 在mingw应用程序中忽略清单
- 如何关闭windows系统中不安全的端口
- Windows的“man”程序在哪里(打开UNIX手册页的程序)?
- microsoft-office-365 – 部署Office 365 Pro Plus时卸载旧
- windows – 使用vbscript激活(带到前台)特定窗口
- windows – remote:fatal:解包后留下未解决的增量
- windows-phone-7 – 在WP7上的“后退”按钮上退出应用程序
- windows-8 – WinRT XAML中的RadialGradientBrush在哪里?
推荐文章
站长推荐
- win10 配置 hadoop-2.7.2
- (转)解决windows10下无法安装.net framework 3.5
- 无法安装Windows 7 SDK(需要已安装的.NET Framew
- windows – 防止以纯文本形式获取IIS物理路径凭据
- win10 jdk的安装和环境变量的配置
- 每日扫盲二:xxx.dll文件的作用
- windows – 不允许具有管理权限的用户执行postgr
- 数据绑定 – 与Windows Phone 8的Map API扩展绑定
- windows-server-2008 – Windows Server 2008,20
- 关于windows下 使用Anaconda3安装的python无法使
热点阅读