How to encapsulate User Setting (Options Page) in Visual Studio 2010 AddIn - visual-studio

I'm currently developping a Visual Studio Extension and I have a question about Options Page. Options Page allows user to save setting about your Extension. Visual Studio handle a lot of work for us.
I created the Options Page.
public class VisualStudioParameter : DialogPage
{
private string _tfsServerUrl = DefaultParameter.TfsServerUrl;
[Category("TFS Parameters")]
[DisplayName(#"Server Name")]
[Description("The URL of your TFS Server")]
public string TfsServerUrl
{
get { return _tfsServerUrl; }
set { _tfsServerUrl = value; }
}
}
First, I created a method in the Visual Studio Package to acces to the Options Page.
Okay so now, from my Package, I can easily acces to the settings.
partial class SpecFlowTfsLinkerExtensionPackage : Package : IParameter
{
....
....
public string GetTfsServerUrl()
{
return ((VisualStudioParameter) GetDialogPage(typeof (VisualStudioParameter))).TfsServerUrl;
}
}
Now, I want to be able, in another library (Another project, included in the VSIX Package), to get easily these values. I don't want to reference the Visual Studio AddIn Package in my library.
I also have Unit Test so I'm going to create an Interface. During Unit Test, I going to Mock the object.
public interface IParameter
{
string GetTfsServerUrl();
}
Do you have any idea about how I can develop a clean solution to get these parameters from another assembly ?
Do you think the better solution is to inject the AddIn dependency in my library ?
If you already developed a Visual Studio Extension, How did you encapsulated the user setting from your core assembly ?
Thanks a lot.

You can try something like that:
// Access DTE infrastructure
EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
// Access options page
var props = dte.get_Properties(#"Your Extension", "General");
var pathProperty = props.Item("TfsServerUrl");
path = pathProperty.Value as string;

Related

How to detect that Library code is executed within VS Addin or VS Package vsix

I use Addin in VS 2010 and VS Package (vsix) in VS 2012.
Addin and VSPackage uses common libraries.
I need detect if the library code (in execution time) is executed by Addin OR VSPackage.
Now, I have this code, but always true for Addin AND VSPackage
public static bool VSAddinVSPackageMode
{
get { return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv"); }
}
I would like
public static bool VSAddinMode { get { ... } }
public static bool VSPackageMode { get { ... } }
Any suggestions for do best way about it?
Add-ins and packages are like different installers for the code. The actual code is the same and executed in the same VS process.
To detect the caller, if you know your host module names, you can walk the call stack until you find the add-in or package module.
You should probably not be using add-ins for Visual Studio 2010 or newer, but that's beside the point.
Add-Ins always use the same entry point. You could use a property like the following:
public bool VSAddinMode
{
get;
internal set;
}
Then, in your OnConnection method, set VSAddinMode = true; before calling other code.
VS Packages do not always have a single entry point, so the best way to implement VSPackageMode is the following:
public bool VSPackageMode
{
get { return !VSAddinMode; }
}

Visual Studio Editor Extension Options Dialog

I have a simple Visual Studio extension that is built in a similar manner as the one presented in this walkthrough (using the IWpfTextViewCreationListener interface).
The extension uses two colors that I'd like to make configurable.
How can I define an Options Dialog for this extension? (for example, a properties page that would appear in the Tools/Options menu)
I have tried to do this using the DialogPage Class, but apparently it requires a VSPackage and I'm not sure if this approach is compatible with what I'm doing.
I think you can make your colors customisable without providing a custom OptionsPage.
You can Export your own colors and they will became configurable from Tools-Options-Fonts and Colors
By your linked example:
[Export(typeof(EditorFormatDefinition))]
[Name("EditorFormatDefinition/MyCustomFormatDefinition")]
[UserVisible(true)]
internal class CustomFormatDefinition : EditorFormatDefinition
{
public CustomFormatDefinition( )
{
this.BackgroundColor = Colors.LightPink;
this.ForegroundColor = Colors.DarkBlue;
this.DisplayName = "My Cusotum Editor Format";
}
}
[Export(typeof(EditorFormatDefinition))]
[Name("EditorFormatDefinition/MyCustomFormatDefinition2")]
[UserVisible(true)]
internal class CustomFormatDefinition2 : EditorFormatDefinition
{
public CustomFormatDefinition2( )
{
this.BackgroundColor = Colors.DeepPink;
this.ForegroundColor = Colors.DarkBlue;
this.DisplayName = "My Cusotum Editor Format 2";
}
}
[Export(typeof(IWpfTextViewCreationListener))]
[ContentType("text")]
[TextViewRole(PredefinedTextViewRoles.Document)]
internal class TestViewCreationListener : IWpfTextViewCreationListener
{
[Import]
internal IEditorFormatMapService FormatMapService = null;
public void TextViewCreated( IWpfTextView textView )
{
IEditorFormatMap formatMap = FormatMapService.GetEditorFormatMap(textView);
ResourceDictionary selectedText = formatMap.GetProperties("Selected Text");
ResourceDictionary inactiveSelectedText = formatMap.GetProperties("Inactive Selected Text");
ResourceDictionary myCustom = formatMap.GetProperties("EditorFormatDefinition/MyCustomFormatDefinition");
ResourceDictionary myCustom2 = formatMap.GetProperties("EditorFormatDefinition/MyCustomFormatDefinition2");
formatMap.BeginBatchUpdate();
selectedText[EditorFormatDefinition.BackgroundBrushId] = myCustom[EditorFormatDefinition.BackgroundBrushId];
formatMap.SetProperties("Selected Text", selectedText);
inactiveSelectedText[EditorFormatDefinition.BackgroundBrushId] = myCustom2[EditorFormatDefinition.BackgroundBrushId];
formatMap.SetProperties("Inactive Selected Text", myCustom2);
formatMap.EndBatchUpdate();
}
}
Custom EFDs can provide SolidColorBrushes.
If this isn't enought, you can also access to the service provider used by VSPackages. You can make a package for the option page, and communicate with the Editor Extension through the service provider with a custom service.
You can import the service provider like this:
[Import]
internal SVsServiceProvider serviceProvider = null;
This soulution also doesn't require from you to migrate the original logic, only the creation of an additional package.
I know this is old, but I thought some of these links might help you (NB: I haven't actually done this myself).
I'd guess you are missing attributes detailed in the MSDN Page:
MSDN - Walkthrough: Creating an Options Page
[ProvideOptionPage(typeof(OptionPageGrid),
"My Category", "My Grid Page", 0, 0, true)]
Some other options are:
Integrating into Visual Studio Settings
Registering Custom Options Pages
If you do decide to go with making a VSPackage (as I believe that's the recommended approach), see this MSDN page and this other SO answer. I also show how to do this using C# and a WPF User Control for the Options Page on my blog here.

Entity Framework Designer Extension Not loading

I created a small extension for the EF designer that adds a new property to the property window. I did this using a vsix project (new project -> c# -> extensibility -> vsix project). When I hit F5 the experimental VS instance starts up. I create a new project, add an entity data model and add an entity. However, my break points never get hit and I don't see the property. Any ideas as to what I might be doing wrong?
public class AggregateRootValue
{
internal static XName AggregateRootElementName = XName.Get("AggregateRoot", "http://efex");
private readonly XElement _property;
private readonly PropertyExtensionContext _context;
public AggregateRootValue(XElement parent, PropertyExtensionContext context)
{
_property = parent;
_context = context;
}
[DisplayName("Aggregate Root")]
[Description("Determines if an entity is an Aggregate Root")]
[Category("Extensions")]
[DefaultValue(true)]
public string AggregateRoot
{
get
{
XElement child = _property.Element(AggregateRootElementName);
return (child == null) ? bool.TrueString : child.Value;
}
set
{
using (EntityDesignerChangeScope scope = _context.CreateChangeScope("Set AggregateRoot"))
{
var element = _property.Element(AggregateRootElementName);
if (element == null)
_property.Add(new XElement(AggregateRootElementName, value));
else
element.SetValue(value);
scope.Complete();
}
}
}
}
[Export(typeof(IEntityDesignerExtendedProperty))]
[EntityDesignerExtendedProperty(EntityDesignerSelection.ConceptualModelEntityType)]
public class AggregateRootFactory : IEntityDesignerExtendedProperty
{
public object CreateProperty(XElement element, PropertyExtensionContext context)
{
var edmXName = XName.Get("Key", "http://schemas.microsoft.com/ado/2008/09/edm");
var keys = element.Parent.Element(edmXName).Elements().Select(e => e.Attribute("Name").Value);
if (keys.Contains(element.Attribute("Name").Value))
return new AggregateRootValue(element, context);
return null;
}
}
EDIT: I put the code on Github: https://github.com/devlife/Sandbox
EDIT: After Adding the MEF component to the manifest as suggested, the extension still never loads. Here is a picture of the manifest:
So the answer, as it turns out, is in how I setup my project. I put both classes inside the project which produces the VSIX file. By simply moving those classes into another project and setting that project as the MEF Component in the manifest (and thus copying the assembly) it worked like a charm!
For VS2012, it is only needed to add Solution as MEF component also. Just add whole solution as MEF component also.
Then it works surprisingly fine.
It seems the dll built by your project isn't automatically included in the generated VSIX package, and VS2013 doesn't give you options through the IDE to change this (that I can work out, anyway).
You have to manually open the project file and alter the XML. The property to change is IncludeAssemblyInVSIXContainer.
Seen here: How to include VSIX output in it's package?

Getting the current project directory from experimental instance of visual studio 2010?

I'm currently implementing uml validation http://msdn.microsoft.com/en-us/library/ee329482.aspx,
when i debug, it opens a new experimental instance of visual studio for me to validate uml diagrams.
Is there a way to get the path of project directory selected by the user when the experimental instance of visual studio is running??
To be more clear,
project A - has VSIX and Class library components to validate uml validation. These class Library components are added to VSIX as MEF components
when i debug Project A -> new experimental instance of VS will open-> Then creating a new project (ctrl+shift+N)-> select modelling project-> browse to the directory (to store the modelling project)->Name the Project as "MYMODEL" -> then press OK
Now, In my Project A i need the path of MYMODEL. Can you please tell me how do i get that path??
Thanks in Advance,
This is a bit roundabout, but works.
You need references to EnvDTE and Microsoft.VisualStudio.Shell.Immutable.10.0 as well as the usual bits.
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.ArchitectureTools.Extensibility;
using Microsoft.VisualStudio.ArchitectureTools.Extensibility.Uml;
using Microsoft.VisualStudio.Modeling.Validation;
using Microsoft.VisualStudio.Uml.AuxiliaryConstructs;
namespace Validation
{
public class MyValidationExtensions
{
[Import]
public Microsoft.VisualStudio.Shell.SVsServiceProvider ServiceProvider { get; set; }
[Export(typeof(System.Action<ValidationContext, object>))]
[ValidationMethod(
ValidationCategories.Open
| ValidationCategories.Menu)]
public void ValidateClassNames
(ValidationContext context,
// This type determines what elements
// will be validated by this method:
IModel elementToValidate)
{
IModelStore store = elementToValidate.GetModelStore();
EnvDTE.DTE dte = ServiceProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
//dynamic projects = dte.ActiveSolutionProjects;
foreach (EnvDTE.Project project in dte.Solution.Projects)
{
IModelingProject mp = project as IModelingProject;
if (mp.Store == store)
{
System.Windows.Forms.MessageBox.Show(project.FullName);
}
}
}
// Add more validation methods for different element types.
}
}

From a visual studio package (VSIX) how do I detect a solution or project build?

From a visual studio package (VSIX) how do I detect a solution or project build?
If you have a Package class in your assembly, you can do:
DTE2 = Package.GetGlobalService(typeof(SDTE)) as DTE2;
Then look at then IsOpen property, to see if the solution is open... the look at the Projects property to find the projects.
However, if you mean you how do I get an event when a solution is opened... then Solutions, for example:
public sealed class MyPackage : Package
{
private DTE m_dte;
protected override void Initialize()
{
IServiceContainer serviceContainer = this as IServiceContainer;
m_dte = serviceContainer.GetService(typeof(SDTE)) as DTE;
var m_solutionEvents = m_dte.Events.SolutionEvents;
m_solutionEvents.Opened += SolutionOpened;
...
}
void SolutionOpened()
{
.... away you go...
}
}
ref: VSIX: Getting DTE object ref: http://msdn.microsoft.com/en-us/library/envdte.solution.aspx
ref: http://msdn.microsoft.com/en-us/library/envdte._solution.projects.aspx
Have a look at DTE.Events.BuildEvents there are events for OnBuildBegin and OnBuildDone.

Resources