引自微信小程序:皆我百晓生:
根据你提供的代码,可能有以下几个问题导致你的属性窗口需要点击两次才能加载属性值:
代码中的CheckItem()方法可能返回的是不同的实例,导致mySelContainer和mySelItems的引用在不同的对象上。你可以尝试将CheckItem()方法的返回值赋值给一个局部变量,在之后的代码中使用该局部变量,以保证引用一致性。
确保PropertyHelper.Load()方法可以正确地加载属性值,并将加载的值赋给obj变量。
在添加属性到属性窗口之前,可以先清空mySelItems列表,然后再添加属性。可以使用mySelItems.Clear()方法来清空列表。
以下是修改后的代码示例:
private void TrackSelection()
{
if (frame == null)
{
var shell = parent.GetVsService(typeof(SVsUIShell)) as IVsUIShell;
if (shell != null)
{
var guidPropertyBrowser = new Guid(ToolWindowGuids.PropertyBrowser);
shell.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fForceCreate, ref guidPropertyBrowser, out frame);
}
}
if (frame != null)
{
frame.Show();
//手动重绘一下窗口
frame.SetProperty((int)__VSFPROPID.VSFPROPID_DocView, null);
}
mySelContainer = new SelectionContainer();
mySelItems = new System.Collections.ArrayList();
PropertyType itemProperties = CheckItem().Properties;
string itemName = CheckItem().Name;
object obj = new object();
CommonFunc comFunc = new CommonFunc();
ItemData checkItem = CheckItem();
string JosnPath = Path.Combine(CommonUrl.CommonFileTableUrl, itemName + ".Json");
switch (itemProperties)
{
case PropertyType.Enum:
{
DataTable objectProperties = new DataBaseCommon().SelectProperties((int)itemProperties);
if (objectProperties.Rows.Count > 0)
{
obj = PropertyHelper.Load(objectProperties.Rows[0]["MetaData"].ToString(), itemProperties);
obj = comFunc.returnObj(obj, checkItem, JsonObjName.JsonObjName_Enum, JosnPath);//给属性模型赋值
}
}
break;
}
//清空mySelItems列表
mySelItems.Clear();
//将属性添加到属性窗口
mySelItems.Add(obj);
mySelContainer.SelectedObjects = mySelItems;
}
希望这些修改能够解决你的问题。如果问题仍然存在,请提供更多相关的代码和错误信息,以便我们能够更好地帮助你。