自在猫先生 2023-12-29 11:12 采纳率: 62.9%
浏览 24
已结题

VSIX插件打开属性窗口的时候无法加载数据 需要点击两次才会显示属性值


我在打开属性窗口的时候 只是展示了属性窗口但是没有加载属性窗口的任何属性
但是我再次打开的时候又加载出来了 第一次打开if (mySelContainer == null) 是没有值的 就加载不出来属性
.NET 4.7.2 vs扩展插件代码  加载的时候再属性一下属性窗口进行数据加载
obj 是有值的 {{PropertyType=Enum, ID=8563a2c1-b023-4c47-846e-898d31ba2093, Name=Enum7, Label=}}
mySelItems 和obj都是有值的但是要这个节点我要点击两次才能加载出来属性值

using SelectionContainer = Microsoft.VisualStudio.Shell.SelectionContainer;
private SelectionContainer mySelContainer;
        private System.Collections.ArrayList mySelItems;
        private IVsWindowFrame frame = null;
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.Add(obj);
              mySelContainer.SelectedObjects = mySelItems;
        }

img

  • 写回答

15条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-12-29 11:36
    关注

    引自微信小程序:皆我百晓生

    根据你提供的代码,可能有以下几个问题导致你的属性窗口需要点击两次才能加载属性值:

    1. 代码中的CheckItem()方法可能返回的是不同的实例,导致mySelContainermySelItems的引用在不同的对象上。你可以尝试将CheckItem()方法的返回值赋值给一个局部变量,在之后的代码中使用该局部变量,以保证引用一致性。

    2. 确保PropertyHelper.Load()方法可以正确地加载属性值,并将加载的值赋给obj变量。

    3. 在添加属性到属性窗口之前,可以先清空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;
    }
    

    希望这些修改能够解决你的问题。如果问题仍然存在,请提供更多相关的代码和错误信息,以便我们能够更好地帮助你。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月5日
  • 创建了问题 12月29日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上