.NET Zone is brought to you in partnership with:

My name is Toni Petrina and I am a software developer and an occasional speaker. Although I primarily develop on the Microsoft stack, I like to learn new technologies. My hobbyist projects range from game development, regardless of the technology, to ALM. I spend most of my time with my girlfriend and someday I will learn how to play the guitar properly. Toni is a DZone MVB and is not an employee of DZone and has posted 69 posts at DZone. You can read more from them at their website. View Full User Profile

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

12.29.2012
| 1991 views |
  • submit to reddit

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]


Published at DZone with permission of Toni Petrina, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)