富文本框是常用的组件之一,多用于文章排版、用户评论等。

WinRT组件中内置了两个:RichEditBox、RichTextBlock。RichEditBox可以编辑,RichTextBlock只用来显示。

由于内置的组件缺少工具栏,故我准备扩展一下,添加一些常用的功能。

测试代码下载

1、首先创建一个 WinRT 类库项目:

2、添加Templated Control:

会自动创建Themes文件夹与Generic.xaml文件:

其中自定义组件的类中会缺少前缀,手动添加即可:

3、设计RichEditBoxX的样式:

 1     <Style TargetType="winrt:RichEditBoxX">
 2         <Setter Property="Template">
 3             <Setter.Value>
 4                 <ControlTemplate TargetType="winrt:RichEditBoxX">
 5                     <StackPanel>
 6                         <StackPanel Orientation="Horizontal">
 7                             <ComboBox Name="txtFontFamily" Width="80"></ComboBox>
 8                             <ComboBox Name="txtFontSize" Width="80"></ComboBox>
 9                             <Button Name="btnWeight" Width="50" Content="B"></Button>
10                             <Button Name="btnStyle" Width="50" Content="I" FontStyle="Italic"></Button>
11                             <Button Name="btnUnderline" Width="50" Content="U"></Button>
12                             <ComboBox Name="txtAlign" Width="50">
13                                 <ComboBox.Items>
14                                     <ContentControl Name="labLeft" Content="Left" Width="50"></ContentControl>
15                                     <ContentControl Name="labCenter" Content="Center" Width="50"></ContentControl>
16                                     <ContentControl Name="labRight" Content="Right" Width="50"></ContentControl>
17                                     <ContentControl Name="labJustify" Content="Justify" Width="50"></ContentControl>
18                                 </ComboBox.Items>
19                             </ComboBox>
20                             <Button Name="btnForeground" Content="Foreground" Width="120"></Button>
21                             <Button Name="btnBackground" Content="Background" Width="120"></Button>
22                             <Button Name="btnImage" Content="Image" Width="80"></Button>
23                             <Button Name="btnVideo" Content="Video" Width="80"></Button>
24                         </StackPanel>
25                         <RichEditBox Name="txtRichEdit" AcceptsReturn="True" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
26                             
27                         </RichEditBox>
28                     </StackPanel>
29                 </ControlTemplate>
30             </Setter.Value>
31         </Setter>
32     </Style>

语法与WPF、Silverlight基本一致,注意添加命令空间的语法有所改变:

xmlns:winrt="using:BrooksCom.WinRT.Control"

目前无法将自定义组件的XAML单独拆分成多个文件,要被迫写在Generic.xaml中,这应该是一个疏忽,希望正式版能改正这个问题。

目前Generic.xaml依然不能可视化设计,对于美工水平相当凑合的我来说,想好看点是种奢望 J

顺便期待下Blend 5正式版,什么时候能发布,好歹把WPF、Silverlight、WinRT、Windows Phone 四种平台集成到一起,不要分为多个了。

比较奇怪的是WinRT中居然没有Label控件,我暂且使用ContentControl代替。

4、设置字体功能使用了WCF服务:

 1     public class SrvFont : ISrvFont
 2     {
 3         public List<string> DoWork()
 4         {
 5             List<string> __list = new List<string>();
 6             InstalledFontCollection fonts = new InstalledFontCollection();
 7             foreach (FontFamily font in fonts.Families)
 8             {
 9                 __list.Add(font.Name);
10             }
11 
12             return __list;
13         }
14     }

使用了C# 5.0的异步方法,WinRT中几乎都是异步方法,以后要适应这种风格了:

 1         protected async override void OnApplyTemplate()
 2         {
 3             base.OnApplyTemplate();
 4 
 5             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
 6 
 7             ComboBox __txtFontFamily = this.GetTemplateChild("txtFontFamily") as ComboBox;
 8             SrvFont.SrvFontClient __proxy = new SrvFont.SrvFontClient();
 9             System.Collections.ObjectModel.ObservableCollection<string> __list = await __proxy.DoWorkAsync();
10             foreach (string s in __list)
11             {
12                 __txtFontFamily.Items.Add(s);
13             }
14 
15             __txtFontFamily.SelectionChanged += __txtFontFamily_SelectionChanged;

5、设置字体、字号等格式:

使用 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat 来设置样式。

 1         void __txtFontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e)
 2         {
 3             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
 4             ComboBox __txtFontFamily = this.GetTemplateChild("txtFontFamily") as ComboBox;
 5             __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Name = __txtFontFamily.SelectedItem.ToString();
 6         }
 7 
 8         void __txtFontSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
 9         {
10             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
11             ComboBox __txtFontSize = this.GetTemplateChild("txtFontSize") as ComboBox;
12             __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Size = float.Parse(__txtFontSize.SelectedItem.ToString());
13         }

字体加粗的枚举比较特殊,类似一个开关:

 1         void __btnWeight_Click(object sender, RoutedEventArgs e)
 2         {
 3             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
 4             Button __btnWeight = this.GetTemplateChild("btnWeight") as Button;
 5 
 6             if (__txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Bold == Windows.UI.Text.FormatEffect.On)
 7             {
 8                 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Bold = Windows.UI.Text.FormatEffect.Off;
 9             }
10             else
11             {
12                 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Bold = Windows.UI.Text.FormatEffect.On;
13             }
14         }
15 
16         void __btnStyle_Click(object sender, RoutedEventArgs e)
17         {
18             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
19             Button __btnStyle = this.GetTemplateChild("btnStyle") as Button;
20             if (__txtRichEdit.Document.Selection.FormattedText.CharacterFormat.FontStyle == Windows.UI.Text.FontStyle.Italic)
21             {
22                 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.FontStyle = Windows.UI.Text.FontStyle.Normal;
23             }
24             else
25             {
26                 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.FontStyle = Windows.UI.Text.FontStyle.Italic;
27             }
28         }
29 
30         void __btnUnderline_Click(object sender, RoutedEventArgs e)
31         {
32             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
33             Button __btnUnderline = this.GetTemplateChild("btnUnderline") as Button;
34             if (__txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Underline == Windows.UI.Text.UnderlineType.None)
35             {
36                 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Underline = Windows.UI.Text.UnderlineType.Single;
37             }
38             else
39             {
40                 __txtRichEdit.Document.Selection.FormattedText.CharacterFormat.Underline = Windows.UI.Text.UnderlineType.None;
41             }
42         }

使用ParagraphFormat.Alignment设置对齐方式:

 1         void __txtAlign_SelectionChanged(object sender, SelectionChangedEventArgs e)
 2         {
 3             RichEditBox __txtRichEdit = this.GetTemplateChild("txtRichEdit") as RichEditBox;
 4             ComboBox __txtAlign = this.GetTemplateChild("txtAlign") as ComboBox;
 5             switch (((sender as ComboBox).SelectedItem as ContentControl).Name)
 6             {
 7                 case "labLeft":
 8                     __txtRichEdit.Document.Selection.FormattedText.FormattedText.ParagraphFormat.Alignment = Windows.UI.Text.ParagraphAlignment.Left;
 9                     break;
10                 case "labCenter":
11                     __txtRichEdit.Document.Selection.FormattedText.FormattedText.ParagraphFormat.Alignment = Windows.UI.Text.ParagraphAlignment.Center;
12                     break;
13                 case "labRight":
14                     __txtRichEdit.Document.Selection.FormattedText.FormattedText.ParagraphFormat.Alignment = Windows.UI.Text.ParagraphAlignment.Right;
15                     break;
16                 case "labJustify":
17                     __txtRichEdit.Document.Selection.FormattedText.FormattedText.ParagraphFormat.Alignment = Windows.UI.Text.ParagraphAlignment.Justify;
18                     break;
19                 default:
20                     break;
21             }
22         }

设置颜色、插入图片、视频等还没有做好,有些问题,以后慢慢完善。

最终效果:

转载于:https://www.cnblogs.com/brooks-dotnet/archive/2012/04/10/2441339.html

开发WinRT自定义组件之富文本框相关推荐

  1. vue--echarts 图标库、excel导出、面包屑组件、富文本框、地图、前端使用代理访问、监控生产环境or开发环境

    目录 一.echarts 图标库 1.echarts的基础 2.项目中的使用 二.execl导出 三.面包屑组件 四.富文本框 五.地图 六.vite 构建配置 七.后端未开跨域资源共享,前端使用代理 ...

  2. ElementUI富文本框组件wangEditor实现

    富文本框wangEditor 需求:用户要求使用富文本框上传图片.文章. 需求分析:wangeditor实现需求,几行代码即可创建一个编辑器. 解决过程: 安装 npm i wangeditor -- ...

  3. 解决Vue用v-html、v-text渲染后台富文本框文本内容样式修改问题,用自定义css样式无法渲染出对应效果的问题

    举例: 如果您要加载富文本框内容的DOM id是detail 那么就这么写scss样式 #detail {font-size: 14px;text-align: center;&>> ...

  4. 基于bootstrap的富文本框——wangEditor【欢迎增加开发】

    先来一张效果图: 01. 引言 老早就開始研究富文本框的东西,在写完<深入理解javascript原型与闭包>之后,就想着要去做一个富文本框的插件的样例.

  5. 基于bootstrap的富文本框——wangEditor【欢迎加入开发】

    先来一张效果图: 01. 引言 老早就开始研究富文本框的东西,在写完<深入理解javascript原型与闭包>之后,就想着要去做一个富文本框的插件的例子.

  6. html加载富文本_Uniapp基础实战富文本框解析 WordPress rest api实例

    文本是更具上篇文章uni-app上下拉刷新的续文有需要了解上文的请点击下面连接访问 传送门: Uni-app实战上加载新下拉刷新 WordPress rest api实例 那么我们就开始了,主要的要是 ...

  7. php文本框长度限制,php截取富文本框中的固定长度的字符

    ai,哎怎么赶脚自己写东西越来越小儿科了呢,现在连这个问题都找了好半天 因为后台是的内容是富文本编辑器编辑的,前台我傻逼的直接截取了字符串,然后样式啥的都乱了,找了半天是因为富文本的问题 其实解决办法 ...

  8. 若依前后端分离发布富文本框内容 | uni-app微信小程序展示富文本框内容

    微信小程序端引入富文本样式 富文本提交图片json error 一.展示示例: 1.PC端前端发布界面 可以设置文字大小,居中,可以插入图片,设置图片大小,居中. 2.小程序端展示 二.基于若依框架踩 ...

  9. vue中如何使用wangEditor 富文本框

    在做后台管理项目时常常会用到富文本编辑器,在这里推荐大家使用wangEditor,亲测好用 话不多说先上图 第一步安装 npm wangeditor --save 第二步在项目中使用 先建立一个wan ...

最新文章

  1. 哪些人适合学习web前端?
  2. Rinne Loves Data Structure
  3. ['1', '2', '3'].map(parseInt) what why ?
  4. linux函数实验报告,linux实验报告
  5. python绘制3d坐标轴_python – 尝试使用matplotlib更新3D图形坐标
  6. 【Numpy学习记录】np.cov详解
  7. Poj3261 Milk Patterns
  8. 搭建Harbor私有仓库
  9. Pandas Index 属性
  10. 试述计算机控制系统的大致组成,试述工业计算机控制系统的组成及应用
  11. 【CVPR2005】梯度方向直方图(Histogram of Oriented Gradients,简称HOG)
  12. Web of science(WOS)引文跟踪
  13. Webstorm2018破解
  14. 企业微信应用消息html标签,消息类型及数据格式
  15. MapReduce之Partition分区实例操作
  16. 全国各省-土地转让收入(1995-2019年)
  17. 【Axure交互教程】 可模糊搜索的多选效果
  18. 仿新版QQ底部导航栏动态拖动按钮
  19. (172)SystemVerilog[打两拍]
  20. VMware部分产品

热门文章

  1. C++类成员的初始化顺序
  2. bzoj 3392: [Usaco2005 Feb]Part Acquisition 交易(最短路)
  3. matlab fspecial
  4. C - 二进制换十进制(简单)
  5. docker教程,dockerfile教程
  6. python基础系列教程——python面向对象编程全解
  7. 安卓application生命周期的onCreate、onLowMemory、onTrimMemory、onConfigurationChanged
  8. jQuery Mobile中列表listview(ol、ul)的data-*选项
  9. 利用vscode插件C51生成C51的hex文件
  10. 汇编:在BUFFER中定义了的十个带符号字,将其中的负数变成绝对值,并以十进制方式输出