Does anyone know how to implement the DynamicEnumProperty type for C++ Project Property rules

China☆狼群 提交于 2019-12-08 04:24:00

问题


I am trying to add a property to our custom build configuration for a C++ project. I want the property combo box to display a dynamic list of values that I can set in code. I think that this should be done using the DynamicEnumProperty type but I am unsure of its implementation. Has anyone worked with this property before that can point me in the right direction?

Thanks


回答1:


In your VSPackage (or any MEF-exposed DLL referenced by it) create a class implementing IDynamicEnumValuesProvider and add [Export(typeof(IDynamicEnumValuesProvider)), DynamicEnumCategory("MyCategory")] to that class's attributes. Then add EnumProvider="MyCategory" to the DynamicEnumProperty definition and your class will be used as the values provider.

Make sure your package references Microsoft.VisualStudio.ProjectSystem.Utilities.v12.0.dll and Microsoft.VisualStudio.ProjectSystem.V12Only.dll (for VS2013) or similar assemblies for earlier versions.




回答2:


I know it's a bit old question... but you might still enjoy the solution ;)

Beside referencing the assemblies and exporting desired type via MEF as Dmitry explained above, you also need to mark the VSPackage as MEF-enabled to make it scan though your contracts. Do it by editing source.extension.vsixmanifest:

for VS2010:

<Content>
  <VsPackage>|%CurrentProject%;PkgdefProjectOutputGroup|</VsPackage>
  <MefComponent>|%CurrentProject%|</MefComponent>
</Content>

for VS2012 / VS2013:

<Assets>
  <Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project"
         d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
</Assets>

This should let you hit a breakpoint set in an exported class.


Additionally, if you need to create an object at runtime 'manually', you can use VisualStudio's internal composition container. The simplest way to access it from anywhere is:

var container = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;
var service = container.GetService<SVsXYZ>();

I will shortly add a sample here: https://github.com/phofman/vs-plugin, so just putting the link for future reference.



来源:https://stackoverflow.com/questions/18090422/does-anyone-know-how-to-implement-the-dynamicenumproperty-type-for-c-project-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!