错误提示:
Cannot change the active view of a modifiable document (with a transaction curently open)
原因在于:视图不能在事务进行时更改,因为事务的运行牵扯到视图,视图正在被利用,所以应该在事务提交之后再更改激活视图!
public static void ActiveViewByName(UIApplication app, string viewname)
{
Document doc = app.ActiveUIDocument.Document;
UIDocument uidoc = app.ActiveUIDocument;
FilteredElementCollector collector
= new FilteredElementCollector(doc)
.OfClass(typeof(View));
foreach (View v in collector)
{
Debug.Assert(null != v,
"never expected a null view to be returned"
+ " from filtered element collector");
// Skip view template here because view
// templates are invisible in project
// browser
if (v.Name == viewname && !v.IsTemplate)
{
uidoc.ActiveView = v;
}
}
}