Installing MVVM Light in Visual Studio with NuGet and the Package Manager Console

I’m an Ubuntu guy so apt-get has my respect. It follows quickly that when Phil Haack and his crew come out with NuGet I’m ready. NuGet should take away one unusual annoyance I’m getting with the MVVM Light binaries installed “by hand”—I’m getting this type-or-namespace-GalaSoft-not-found error continually! I have to manually Add Reference… and rummage through the file system to refresh the Visual Studio project links the binaries. My optimistic assumption is that the NuGet packaging of MVVM Light will prevent this strange error and save me from rummaging.

After reading “Finding and Installing a NuGet Package Using the Package Manager Console” I used this:

PM> Get-Package -remote -filter MvvmLight

A PowerShell table formatted for the console should return one row with the MvvmLight package information—make sure that Package source: is set to All in the Package Manager Console. Running the Install-Package command will install the relevant MVVM Light binaries (for Silverlight or WPF) into the Visual Studio project selected in the Default project: combo box.

In my case I would have to run Install-Packagefour times like this:

Install-Package MvvmLight

Each time I would have to change project the Default project: combo box. Using the, er, power of PowerShell, these are the four projects I’m talking about:

PM> Get-Project -all | where {$_.Name -match "Songhay.Silverlight" -and $_.Name -notmatch "ApplicationLoader" -and $_.Name -notmatch ".Xml"} | format-table Name
Name
----
Songhay.Silverlight
Songhay.Silverlight.Mvvm
Songhay.Silverlight.BiggestBox.IndexPart
Songhay.Silverlight.BiggestBox

So let’s make life a tad easier: let’s list all projects, filter this list and loop through the filtered output, running the Install-Package command:

PM> Get-Project -all | where {$_.Name -match "Songhay.Silverlight" -and $_.Name -notmatch "ApplicationLoader" -and $_.Name -notmatch ".Xml"} | ForEach-Object {Install-Package MvvmLight -project $_.Name}

Related Links

“C#er : IMage: An Ultra Light Windows Phone 7 MVVM Framework” and other links…

Jeremy Likness: “Next comes the fun part. There may be a case for an inversion of control container here, but I’m going to follow a fairly common pattern but instead of shoving a bunch of variables into the App object, I’ll create a GlobalManager class for the MVVM concerns. Now this is a static class and of course that concerns some people for becoming problematic down the road because the views will be tightly bound (plus it’s one of those hated singletons). In this case it just doesn’t bother me. I unit test the view models, not necessarily the views, and use design-time data in the views. Therefore, the binding for me doesn’t pose a threat as I can still grab the view models and run them in isolation while mocking the interfaces.”

Also: “C#er : IMage: A Silverlight MVVM Feed Reader from Scratch in 30 Minutes

“Navigation in a #WP7 application with MVVM Light”

Laurent Bugnion: “In his excellent series ‘MVVM Light Toolkit soup to nuts’, Jesse Liberty proposes one approach using the MVVM Light messaging infrastructure. While this works fine, I would like to show here another approach using what I call a ‘view service’, i.e. an abstracted service that is invoked from the viewmodel, and implemented on the view.”

“Coding4Fun Windows Phone Toolkit”

Greg Duncan: “Need an ‘About’ for your Windows Phone 7 app? Toggle button? TimeSpan Picker? A number of value to visibility converters? Why re-invent the wheel, when the Coding4Fun team, having run into their own challenges in building Windows Phone 7 app’s, have built them for you?”

“Windows Azure Emulators On Your Desktop”

Buck Woody: “Many people feel they have to set up a full Azure subscription online to try out and develop on Windows Azure. But you don’t have to do that right away. In fact, you can download the Windows Azure Compute Emulator – a ‘cloud development environment’ – right on your desktop.”

“Microsoft: ‘over 2 million’ Windows Phone 7 licenses sold to manufacturers so far”

Chris Ziegler: “What does that tell us? Well, let’s get the elephant in the room out of the way: the iPhone 4 sold 3 million units in a little under a month after its launch, so Microsoft clearly has plenty of room to catch up—but that comes as no surprise to us, analysts, or Microsoft itself. Furthermore, selling a license to an OEM isn’t the same as selling a phone to a customer, since many of these manufactured devices are sitting on store shelves; it’s unclear exactly how many WP7 devices are actually in users’ pockets right now, but the number is certainly less than ‘over 2 million.’”

“Tips & Tricks: How to get the Screen Resolution in Silverlight?” and other Google Starred Items

 Kunal Chowdhury: “Now from that object [System.Windows.Browser.HtmlPage.Window], you can call the Eval() with proper parameter ‘screen.width’ or ‘screen.height’ to get the Screen Width or Screen Height respectively. Those will give you the Screen Resolution. Suppose, you received Width = 1024 & Height = 768, means your Screen Resolution is: 1024 x 768.”

“Silverlight 4 Unleashed table of content”

Laurent Bugnion: “Because I didn’t want to merely write an update to the existing Silverlight 2 Unleashed, I proposed a different approach to my editor: Using Silverlight 2 Unleashed (which was bringing people up to speed with Silverlight 2 pretty much from scratch) as the basis for the new book, and building on the foundation. The thing is, much of the material from Silverlight 2 Unleashed is still valid in Silverlight 4. There are very few differences and of course all the new features that were added in SL3 and SL4, but let’s face it, I don’t have much new to write about XAML, the basic transforms, animations etc.”

“Silverlight PivotViewer Control is Here!”

Silverlight Team Blog: “Because PivotViewer leverages Deep Zoom, it displays full, high-resolution content without long load times, while the animations and natural transitions provide context and prevent users from feeling overwhelmed by large quantities of information. This simple interaction model encourages exploration and longer audience engagement times, across a wide array of business model opportunities such as advertising, online retail experience and more.”

“State of Data Access in Silverlight 4”

Shawn Wildermuth: “The three options for data access are still the same ‘Web Services, WCF Data Services and WCF RIA Services’. My major issue with data access today in Silverlight persists: there is no perfect answer. There is give and take with each approach and giving good advice about the right one is difficult. [chart]”

“Visual State Manager A-Z”

Jesse Liberty: “Let’s start with the form that is used in my Getting Started With Silverlight video: Adding Controls To An Application.  That video uses the design surface in Visual Studio 2010, and a subsequent video creates the grid in Xaml. To round things out I’ll create this version in Expression Blend. [video]”

“Creating a Netflix App using nRoute: A Step-by-Step Guide”

Rishi: “With the latest release of nRoute we’ve added a trove of navigation-related features, to explain and demonstrate these features I’ve put together an end-to-end application that fronts the Netflix’s oData Catalog. And so in this post we’ll go through using these feature in a step-by-step manner to cumulatively compose a rich, extensible, and loosely-coupled application.”

“Dual Targeting WPF and Silverlight (and Windows Phone 7)”

Pete Brown: “At DevLink 2010, I presented my ‘Dual Targeting’ talk. I’ve presented this talk at a few conferences. With the addition of WP7, maybe I need to change it to ‘multi-targeting’ as xaml hits more platforms :) [slides]”

“10 things Beginning Silverlight and WPF Developers Need to Know”

Pete Brown: “The 10 things are: 9. How to Hand-Code XAML 8. How to Use Expression Blend 7. How to Write Value Converters 6. Threading 5. Async Programming 4. An Architectural Pattern (like MVVM or similar) 3. The Layout System 2. Dependency Properties 1. Binding 0. Our limitations, and when to call in a designer. [slides]”

“Building a DataGrid Control for Silverlight for Windows Phone”

Walter Ferrari: “The good news is that the way is much simpler than the one described in the previous paragraph. The bad news is that the source code for the current Toolkit’s DataGrid is targeted for Silverlight 4 whilst Windows Phone 7 runs a customized version of Silverlight 3 with just some capabilities of Silverlight 4. But do not worry, you can retrieve an older release from CodePlex…”

“New Silverlight Video Tutorial: How to Create a ListBox with a Static Background”

Victor Gaudioso: “Someone from my Silverlight Email List emailed me and told me that he had been struggling trying to create a ListBox with a static background image. So, I made this short, 3 minute tutorial I show you how to do it all in Blend. [video]”

“Using The Silverlight DataGrid with View Model / MVVM”

Michael Washington: “The Silverlight DataGrid is a powerful control that allows inline editing, paging, sorting, and drag and drop re-ordering of columns. It can be challenging to understand when using normal code behind techniques, but sometimes downright baffling when using View Model / MVVM style programming. [CodeProject.com]”

FunkyKB: “Silverlight’s CollectionViewSource” and other items

Bea Stollnitz: “CollectionViewSource was added to WPF mainly to permit view-related scenarios to be expressed in XAML (so they could be tooled), and also to aid in remembering the current item of previously displayed collections. The reasons for adding CollectionViewSource to Silverlight were very different. Before the introduction of CollectionViewSource, Silverlight collections were not automatically wrapped by views – in fact the concept of “views” didn’t even exist in Silverlight 2. There was no way to sort or filter a collection (other than modifying the collection itself), or to build a master-detail scenario based on currency (although you could create a master-detail scenario based on selection). …Unlike WPF, Silverlight only wraps collections with a view when CollectionViewSource is used as an intermediary. If you simply bind an ItemsControl to a collection, no ‘default view’ will be created internally for you.”

“Data Binding Changes in Silverlight 4”

Shawn Wildermuth: “Data binding has also added the ability to specify what to show in certain cases. These cases include when the value is NULL and when the value fails to load. There are two markup extensions that support this. First the TargetNull markup extension lets you specify what to show when the bound value is null. Secondly, the FallbackValue is used in the same way but is shown when the value cannot be loaded through data binding (normally when the DataContext is null or fails to find the property on the bound object). ”

“Silverlight 3’s New Child Windows”

Jeff Prosise: “The new child window feature makes it easy to add modal dialogs to Silverlight applications. A child window derives from the new ChildWindow class, and its content is defined in XAML (of course!). So you can now embellish an application with modal popups containing rich content.” This one I overlooked inside the Silverlight 4 timeframe.

“Braindump on the Event Aggregator Pattern”

Jeremy D. Miller: “Many people first come into contact with the Event Aggregator pattern through the implementation in Prism.  For my regular readers you may be shocked (shocked I say!) to know that I don’t particularly care for the way they implemented the pattern in Prism.  I think the Prism implementation is clumsy and awkward to use.  I despise that idiom of first getting the event aggregator, then retrieving an ‘Event’ object that I’ll then listen to.  Same thing with publishing.  I think it’s awkward that I have two steps (get event, then publish) instead of just saying ‘IEventAggregator.Send().’” My current preference is to use the ‘light’ messaging in Laurent Bugnion’s MVVM Light.

My Noted Differences between Silverlight and WPF

Libraries

  • The DLL %SystemRoot%\Microsoft\Framework\v3.0\PresentationCore.dll is not a part of Silverlight (PresentationCore.dll can be thought of as the heart of WPF). This means that the System.Windows.* namespaces are not a part of WPF.
  • Silverlight uses %ProgramFiles%\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\System.Xml.dll (version 2.0.5.0) not the one used by managed Windows Applications (%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll). The first implication here is major: the System.Xml.XPath is missing from Silverlight 3.0 (but kind of returns in Silverlight 4!).
  • Silverlight uses %ProgramFiles%\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\system.dll (version 2.0.5.0 in lowercase). This implies that Josh Smith’s WPF-based ViewModelBase.VerifyPropertyName() (announced on his Blog and published in his MSDN article) method does not work because the TypeDescriptor class is not in Silverlight. A Silverlight-based equivalent published by Shawn Wildermuth for MSDN works without verifying property names.
  • There is no Silverlight version of StructureMap (2.5.x). This actually makes Unity superior for the moment. Jeremy Miller is thinking about this

Visuals

  • The HyperlinkButton control is exclusively a part of Silverlight. This may be to take the place of the Hyperlink control in WPF (the Hyperlink control is defined in the System.Windows.Documents namespace—which is not a part of Silverlight 3). Silverlight, then, makes controls that derive from ButtonBase have a near monopoly on click-like events. This explains (to me) why Laurent Bugnion, author of the MVVM Light Toolkit, defines commanding extension methods (GalaSoft.MvvmLight.Command.ButtonBaseExtensions) exclusively for ButtonBase.
  • Controls like GridSplitter are in the Silverlight 3 SDK (%ProgramFiles%\Microsoft SDKs\Silverlight\v3.0\Libraries\Client\System.Windows.Controls.dll)—along with Calendar, DatePicker, HeaderedItemsControl, and more. By the way you might get the AG_E_UNKNOWN_ERROR because of Style Setter attributes not parsing at runtime.
  • There is no concept of a startup Window in Silverlight Navigation Applications. A root UserControl is defined instead. Silverlight places great emphasis on the concept of the “root visual” (Application.Current.RootVisual).
  • The attribute TargetType="{x:Type StackPanel}" is not supported in Silverlight (x:Type syntax is not supported). Use TargetType="StackPanel" instead.
  • The GridSplitter control apparently is not available for Silverlight by default. It’s in the Silverlight 3 SDK (%ProgramFiles%\Microsoft SDKs\Silverlight\v3.0\Libraries\Client\System.Windows.Controls.dll).

Links to Selected Resources

WPF Compatibility Might be slightly out of date for Silverlight 3: “Silverlight does not support dynamic resources. All resource references to keyed resources in XAML are static. … Silverlight introduces the VisualStateManager, which helps define and manage the visual behavior based on the control state. For example, “Pressed” might be defined as a button state when the button is clicked. By using VisualStateManager, you can define all the control states and the transitions between states in your control templates. For more information, see Customizing the Appearance of an Existing Control by Using a ControlTemplate.”
What features of WPF are excluded in Silverlight 3? Links to MSDN article (see below).
XAML Processing Differences Between Silverlight and WPF “All material in this section applies specifically to Silverlight version 3 and its implementation of the XAML parser provided with that release. Also, processing XAML for the JavaScript API in Silverlight is not described here, because the JavaScript-specific XAML behaviors in Silverlight are not relevant for comparison with WPF.”
Control Styles and Templates Can’t find the WPF equivalent of this: “Silverlight uses default styles and templates for several controls. You can copy these styles and templates into your project and modify them in order to customize control behavior and appearance.”
Security Considerations for Reflection “In Silverlight, you cannot use reflection to access private types and members. If the access level of a type or member would prevent you from accessing it in statically compiled code, you cannot access it dynamically by using reflection.”
Using RelayCommands in Silverlight 3 and WPF “In Silverlight, parts of the “plumbing” are available already (the ICommand interface) but most of it is missing. This led some very clever people to implement Commands in Silverlight, to replace the missing pieces by a custom implementation. In Prism, the command implementation is called DelegateCommand. In my MVVM Light Toolkit, I use a command that my good friend, colleague at IdentityMine and fellow WPF Disciple Josh Smith created, called the RelayCommand.