DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Extending Team Explorer 2012 – Detecting Connection and using MVVM [2 of N]

Toni Petrina user avatar by
Toni Petrina
·
Dec. 29, 12 · Interview
Like (0)
Save
Tweet
Share
2.92K Views

Join the DZone community and get the full member experience.

Join For Free

Before moving on with other things we may wish to extend, let’s upgrade the trivial addin from the previous post. Since we are integrating with Team Explorer, it would be great to know to which project we are currently connected. If we are aware of the current connection, we can hide the navigation item. Once we regain connection we simply show it again. Since this requires us that we update the UI, and as you’ve seen the UI requests data from us, you will see how MVVM is used in the navigation item to alert Team Explorer pane that something has changed in our UI.

I have created a brand new VSIX project and library named Example2.VSExtension and Example2.ContextSensitiveNavigationItem respectively. I will do this for every blog post since this way I will not merge two separate concepts in one addin. Add a new class to the project, in my case it is named ContextSensitiveNavigationItem, and inherit from ITeamExplorerNavigationItem. Also add the necessary attribute and implement the interface in the same fashion as seen in the previous post. In order to be aware of the current TFS context, we need additional references. Add references to the following assemblies:

  • Microsoft.TeamFoundation.Client
  • Microsoft.Visualstudio.Shell.Immutable.10.0
  • System.ComponentModel.Composition

We can get the information from the service named ITeamFoundationContextManager. To get services in general inside Visual Studio, you need to use MEF and first import the service provider. Once you have it, you can ask it to give you the context manager. The following code demonstrates how to use the service provider:

[Import]
public SVsServiceProvider ServiceProvider { get; set; }
private ITeamFoundationContextManager _contextManager;

public void Invalidate()
{
    if (_contextManager == null && ServiceProvider != null)
    {
        _contextManager = ServiceProvider.GetService(typeof(ITeamFoundationContextManager)) as ITeamFoundationContextManager;
        if (_contextManager != null)
        {
            _contextManager.ContextChanged += _contextManager_ContextChanged;
            UpdateText(_contextManager.CurrentContext);
        }
    }
}

void _contextManager_ContextChanged(object sender, ContextChangedEventArgs e)
{
    UpdateText(e.NewContext);
}

private void UpdateText(ITeamFoundationContext teamFoundationContext)
{
    if (teamFoundationContext == null || string.IsNullOrEmpty(teamFoundationContext.TeamProjectName))
        Text = string.Empty;
    else
        Text = "Plugin connected to " + teamFoundationContext.TeamProjectName;
}

When we aren’t connected to the server at all, we should hide the navigation item since it probably doesn’t make sense to have it yet.

tfs11.extensibility.2.1

The code above caches the context manager inside a class field. Every time Invalidate is called, it will attempt to cache it if it wasn’t already. It will also register an event handler for the event which will fire every time you connect to or disconnect from a TFS project. This way we can be sure that the current project is changed. Changing the caption is a little bit complicated. If there is no caption, e.g. when we are not connected to any project, we also need to hide the navigation item. We also need to fire PropertyChanged whenever we change a property so that Team Explorer can update the UI. When we toggle visibility or change the caption, we need to inform TE that it is changed and that it needs to get the latest version.

Here is the full code for both properties:

private string _text;
private bool _isVisible;

public bool IsVisible
{
    get { return _isVisible; }
    set
    {
        if (_isVisible == value)
            return;
        _isVisible = value;
        RaisePropertyChanged("IsVisible");
    }
}

public string Text
{
    get { return _text; }
    set
    {
        if (_text == value)
            return;

        _text = value;
        IsVisible = !string.IsNullOrEmpty(_text);
        RaisePropertyChanged("Text");
    }
}

We need to keep a backing field for the property and fire the event if the setter detects that the new value is different from the old value. The implementation of the missing function is given below.

private void RaisePropertyChanged(string property)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(property));
}

Once we connect to the project, the navigation item is shown and the caption contains project name.
tfs11.extensibility.2.2

This simple addin demonstrates how to detect the current team explorer context and to act on it. We will need the current context in lots of cases. It also demonstrates how to use the built in MVVM model that Team Explorer relies on. In the next post I will show how to add new pages to Team Explorer.

Entries in this series:
  1. Extending Team Explorer 2012 - Introduction [0 of N]
  2. Extending Team Explorer 2012 - Adding navigation item [1 of N]
  3. Extending Team Explorer 2012 - Detecting connection and using MVVM [2 of N]


teams Connection (dance)

Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is Policy-as-Code? An Introduction to Open Policy Agent
  • What Was the Question Again, ChatGPT?
  • Taming Cloud Costs With Infracost
  • The Importance of Delegation in Management Teams

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: