NewParent
内容
[
隐藏
]NodeChangingArgs.NewParent property
获取操作完成后将设置的节点的父节点。
public Node NewParent { get; }
例子
演示如何在编辑文档树时使用 NodeChangingCallback 实时监视文档树的更改。
public void NodeChangingCallback()
{
Document doc = new Document();
doc.NodeChangingCallback = new NodeChangingPrinter();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");
builder.StartTable();
builder.InsertCell();
builder.Write("Cell 1");
builder.InsertCell();
builder.Write("Cell 2");
builder.EndTable();
#if NET48 || JAVA
builder.InsertImage(Image.FromFile(ImageDir + "Logo.jpg"));
#elif NET5_0_OR_GREATER || __MOBILE__
using (SKBitmap image = SKBitmap.Decode(ImageDir + "Logo.jpg"))
builder.InsertImage(image);
#endif
builder.CurrentParagraph.ParentNode.RemoveAllChildren();
}
/// <summary>
/// 打印文档中发生的每个节点插入/删除。
/// </summary>
private class NodeChangingPrinter : INodeChangingCallback
{
void INodeChangingCallback.NodeInserting(NodeChangingArgs args)
{
Assert.AreEqual(NodeChangingAction.Insert, args.Action);
Assert.AreEqual(null, args.OldParent);
}
void INodeChangingCallback.NodeInserted(NodeChangingArgs args)
{
Assert.AreEqual(NodeChangingAction.Insert, args.Action);
Assert.NotNull(args.NewParent);
Console.WriteLine("Inserted node:");
Console.WriteLine($"\tType:\t{args.Node.NodeType}");
if (args.Node.GetText().Trim() != "")
{
Console.WriteLine($"\tText:\t\"{args.Node.GetText().Trim()}\"");
}
Console.WriteLine($"\tHash:\t{args.Node.GetHashCode()}");
Console.WriteLine($"\tParent:\t{args.NewParent.NodeType} ({args.NewParent.GetHashCode()})");
}
void INodeChangingCallback.NodeRemoving(NodeChangingArgs args)
{
Assert.AreEqual(NodeChangingAction.Remove, args.Action);
}
void INodeChangingCallback.NodeRemoved(NodeChangingArgs args)
{
Assert.AreEqual(NodeChangingAction.Remove, args.Action);
Assert.Null(args.NewParent);
Console.WriteLine($"Removed node: {args.Node.NodeType} ({args.Node.GetHashCode()})");
}
}
也可以看看
- class Node
- class NodeChangingArgs
- 命名空间 Aspose.Words
- 部件 Aspose.Words