问题
I am struggling getting to the contents (children) of a Container using C#. Works fine if just drawn on the page, but once converted to a master, it is no longer recognized as a 'container'.
So, for example. Create a new container from the 'Insert' tab. From the 'Home' tab, draw 2 rectangles in the container, type some text in each, and then 'add to underlying container'.
After this, the code below reports the 2 rectangle Names & Texts.
However, I now drag the container into my stencil to create a master. Then I drag the master to the page to create an instance.
Now the code does not find any MemberShapes at all.
What am I doing wrong ? Any advice/guidance appreciated !!
foreach (Microsoft.Office.Interop.Visio.Shape shape in Globals.ThisAddIn.Application.ActivePage.Shapes)
{
System.Diagnostics.Debug.WriteLine(shape.Name);
if (shape.ContainerProperties != null) {
foreach (int memberID in shape.ContainerProperties.GetMemberShapes(0)) {
Microsoft.Office.Interop.Visio.Shape vsoShape = Globals.ThisAddIn.Application.ActivePage.Shapes.ItemFromID[memberID];
Debug.Print(vsoShape.Name);
Debug.Print(vsoShape.Text);
}
}
}
回答1:
It's your master creation that's giving you trouble I suspect. If you drag your new container onto your doc stencil to create a master the member shapes come along for the ride. However, Visio likes masters to be grouped and if they're not it will group them on drop. If you ungroup your new instance shape (container and members) and then run your code again it should report as expected. The problem with this is that it breaks the link with the master.
I think you'll need to break up your shapes up into separate member and container masters.
If you were dropping a List type container shape then you could instruct Visio to drop additional list item shapes using the User.msvSDListItemMaster + EventDrop combo. (See "Specifying a shape to insert into lists" section of the Custom Containers, Lists and Callouts in Visio 2010 post for more details.) Unfortunately this doesn't work for straight container shapes as the drop position of the new member shapes isn't known.
If you want to have a prepopulated container then one option would be to either listen for the drop event in code and add the new members in the handler, or, fire a MarkerEvent with the QUEUEMARKEREVENT ShapeSheet function from the EventDrop cell. Again, this implies that you'll then need to be listening for and process Application Marker Events.
I don't know how familiar you are with Visio events, but I covered them in the code part of the videos found here:
http://visualsignals.typepad.co.uk/vislog/2016/04/new-visio-training-videos.html
Update 13th Dec 2017 - adding associated course files post as well, which demos code used in course:
http://visualsignals.typepad.co.uk/vislog/2017/01/course-files-for-visio-flyby-for-developers-on-ch9.html
来源:https://stackoverflow.com/questions/47652649/c-sharp-visio-containers