问题
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