以编程方式构建RunProperties [英] Building RunProperties programmatically

查看:110
本文介绍了以编程方式构建RunProperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi group,
我从OpenXML SDK的第2版开始,我有一个关于以编程方式构建RunProperties对象的问题。假设我使用以下代码为运行属性对象添加粗体和下划线:

RunProperties rPr = new RunProperties();
rPr.Append(new Bold());
Debug.Assert (rPr.Bold!= null);
rPr.Append(new Underline());
Debug.Assert(rPr.Underline!= null);

这里的断言没有失败, rPr对象变为粗体和下划线。相反,如果我反转添加子项的顺序,例如:

rPr = new RunProperties ();
rPr.Append(new Underline());
Debug.Assert(rPr.Underline!= null);
rPr.Append(new Bold());
Debug.Assert (rPr.Bold!= null);

第二个断言FAILS,即使孩子数是2而不是1.那么应该如何做呢?是否有以编程方式构建此类对象的预期方法?
Thanx!

解决方案

Open XML架构要求'Bold'元素为在"下划线"元素之前。因此,如果您在"下划线"之后附加"Bold"元素节点,则API将无法将该元素标识为属性值。


听起来有点复杂,但你可以一直使用强类型属性并编写如下代码:


< span style ="font-size:9pt; font-family:新宋体"> RunProperties rPr = new RunProperties ();


rPr.Bold = new Bold ();


rPr.Underline = new 下划线();




< p align = left>问候,


- L。


Hi group,
I'm beginning with version 2 of the OpenXML SDK and I've a question about building a RunProperties object programmatically. Say I use the following code to add bold and underline to a run properties object:

RunProperties rPr = new RunProperties();
rPr.Append(new Bold());
Debug.Assert(rPr.Bold != null);
rPr.Append(new Underline());
Debug.Assert(rPr.Underline != null);

Here the assertions do not fail and the rPr object gets both bold and underline. If instead I invert the order in which children are added, like:

rPr = new RunProperties();
rPr.Append(new Underline());
Debug.Assert(rPr.Underline != null);
rPr.Append(new Bold());
Debug.Assert(rPr.Bold != null);

The 2nd assertion FAILS even if the children count is 2 instead of 1. So how should this be done? Is there an intended way of building such objects programmatically?
Thanx!

解决方案

Open XML schema requires the element for 'Bold' to be before the element for 'Underline'. So if you append the element node for 'Bold' after that of the 'Underline', the API won't be able to identify the element as the property value.

Sounds a bit complex, but you could use the strong typed property all along and write the code as the following:

            RunProperties rPr = new RunProperties();

            rPr.Bold = new Bold();

            rPr.Underline = new Underline();

 

 

Regards,

--L.


这篇关于以编程方式构建RunProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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