本文介绍了修改基础文件后,如何以编程方式刷新/重新加载VS项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Visual Studio程序包,并且编写了一些代码,这些代码将使解决方案资源管理器中的文件依赖于另一个文件.

I am developing a Visual Studio package and I have written some code that will make a file in Solution Explorer dependant upon another file.

这意味着它赋予它们与代码隐藏文件或设计器文件相同的关系,它们在父文件下嵌套显示,带有加号/减号图标.

What this means is that it gives them the same relationship as code-behind files or designer files, where they appear nested under the parent file with a plus/minus icon.

+ MainForm.cs


- MainForm.cs
   MainForm.Designer.cs
   MainForm.resx

我已经成功编写的代码正确地修改了基础项目文件,但是更改只有在关闭并重新打开项目后才会反映在解决方案资源管理器中.

The code that I have written successfully and correctly modifies the underlying project file, however the change is not reflected in Solution Explorer until the project is closed and re-opened.

我正在寻找一些刷新或重新加载项目的代码,以便可以在解决方案资源管理器中立即看到更改.

I'm looking for some code that will refresh or reload the project so that the change is visible in Solution Explorer immediately.

更多信息...

以下是sudo代码,展示了我创建依赖文件的机制.

Here is the sudo code that demonstrates the mechanism by which I create the dependant file.

IVsBuildPropertyStorage vsBuildPropertyStorage = GetBuildPropertyStorage();
vsBuildPropertyStorage.SetItemAttribute(projectItemIdentifier, "DependentUpon", parentFileName);

我还尝试添加此内容,以尝试重新加载项目,但这没有任何效果.

I have also tried adding this in an attempt to get the project to reload, but it doesn't have any effect.

project.Save();
VSProject obj = project.Object as VSProject;
obj.Refresh();

推荐答案

AFAIK唯一的方法是通过自动化解决方案资源管理器工具窗口:

AFAIK the only way of doing this is via automation of the Solution Explorer tool-window:

EnvDTE.DTE dte = ...;

string solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FullName);
string projectName = project.Name;

dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
((DTE2)dte).ToolWindows.SolutionExplorer.GetItem(solutionName + @"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);

dte.ExecuteCommand("Project.UnloadProject");
dte.ExecuteCommand("Project.ReloadProject");

请注意,如果尚未保存项目,则用户将在"Project.UnloadProject"调用之前得到一个对话框.

Note that, if the project hasn't been saved, the user will get a dialog box prior to the "Project.UnloadProject" call.

这篇关于修改基础文件后,如何以编程方式刷新/重新加载VS项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 09:12