If you develop applications using Prism, there have probably been a number times where you said, “Man, I wish I could just click File –> New –> Prism Application, and have Visual Studio create all that monkey code for me”. I mean, let’s face it, a lot of the the code you write to setup a Prism app is pretty repetitive. Not much really changes when setting up a new app. A lot of times you have to add very common items to an existing app, which may be views that you need to add the ViewModelLocator attached property, adding a ViewModel that implements BindableBase, or even adding a new module.
Well, I am happy to announce, that now you can automate all that setup code right within Visual Studio. We have released a new VSIX to the Visual Studio Gallery called the Prism Template Pack. I actually released the Prism Template Pack about a month back as a v1 just to get some feedback from the community before writing a blog about it. Well, the feedback has been great, and now we are at v1.3. What do you get with the new Prism Template Pack? Well, it depends on the platform.
Let’ take a look at what you get in the current 1.3 version of the Prism Template Pack.
Snippets are a core concept in Visual Studio and have been for a long time. I use them constantly, and they really save time stubbing out a lot of verbosity in your code very quickly. We have recognized three snippets of code that are used the most in every Prism application.
The propp snippet creates a public property, backed by private fields, with a getter and setter that uses the SetProperty method from the Prism.Mvvm.BindableBase class. When you use this snippet, the follow code will be created for you.
The cmd snippet will create public property of type DelegateCommand with a getter and private setter. When you use this snippet, the following code will be created.
The cmdg snippet is very similar to the cmd snippet except that this snippet uses the generic version of the DelegateCommand. When you use this snippet, the following code will be generated.
Item templates are probably the most used item in Visual Studio, and you may not even realize it. Ever time you add a new item to your project inside Visual Studio, you are using an item template. The Prism Template Pack provides a number of item templates depending on the platform.
Every platform will have access to the Prism ViewModel item template. This item template will create a new class that derives from Prism.Mvvm.BindableBase with an empty default constructor. When you add tis item to your project, this is the class that will be created.
Now, when building a WPF Prism application there are two major items that we identified that should be an item template.
- Prism UserControl – this will create a WPF UserControl with the Prism namespace defined as well as the ViewModelLocator.AutowireViewModel attached property set to true.
- Prism Window – this item will create a WPF Window with the Prism namespace defined as well as the ViewModelLocator.AutowireViewModel attached property set to true.
I think one of the biggest benefits of the Prism Template Pack item templates comes to the Xamarin.Forms platforms. That’s because Xamarin’s item template suck! Hell, when you select a “XAML Page”, the default file name ends in .CS and not .XAML. How hilarious is that? So to help speed up the creation of the various pages types in Xamarin.Forms, the Prism Template Pack provides these item templates.
- Prism ContentPage – ContentPage with ViewModelLocator
- Prism NavigationPage – NavigationPage with ViewModelLocator
- Prism MasterDetailPage – MasterDetailPage with ViewModelLocator
- Prism TabbedPage – TabbedPage with ViewModelLocator
- Prism CarouselPage – CarouselPage with ViewModelLocator
Project templates are used to get your project started or to add additional assemblies to a current project in your solution. I would like to point out that Project Templates aren’t meant to be full blown sample app with a lot of code and functionality added to teach you how to use something. They are only meant to get you up and running with the bare minimum code required without making too many assumptions about how you are going to architect or write your app.
For WPF we have two project templates.
- Prism Unity App – this is a project template that essentially creates a new WPF shell application and uses Unity as the container. It will have a basic bootstrapper that is responsible for initializing the app, and showing the shell. It will have a MainWindow and a MainWindowViewModel located in the Views and ViewModels folders respectively.
- Prism Module – this project template will add a new project to your solution that will act as a Prism module. It will have a class defined that implements IModule with two empty folders for your Views and ViewModels.
Since Xamarin.Forms doesn’t have the concept of modularity (not yet anyways), there is only one project template.
- Prism Unity App – this project template will create a Xamarin.Forms application with four projects; a PCL project for shared code, an iOS app, an Android app, and a Windows Phone app that all use Unity as the conatiner.
Last but not least, we have editors. By that, I mean custom editors written specifically to help you be more productive creating your Prism applications. I am actually the most excited about this one, because I got to learn something new in order to create it. So far I only have one editor, and that is the new App.config Prism Module Editor. Hand writing XML in your App.config can be a pain in the ass, and it’s not type safe so it is extremely easy to get the syntax wrong, or introduce any number of typos that will cause your app to fail to load your modules and flat out crash.
Since WPF is the only platform that supports having an App.config file act as a module catalog, it is only useful in WPF. In order to use it simply right-click your App.config file, and select Open With Prism Module Editor.
When you click the menu option, a new tab will open in Visual Studio with a list of modules currently defined, if any, and allow you to edit, add, and delete your modules and their dependencies.
What’s really cool about this module editor is that it will automatically scan your solution for projects that have a class that implement IModule, and populate the AssemblyFile dropdown with your available options.
What’s even cooler is that when you selected a project from the dropdown, it automatically fills in the other fields so you don’t have to write all the stupid fully qualified type information.
Now, I didn’t concentrate a ton on the UI side of it. I really just wanted to get something out here as soon as possible. Maybe a grid sin;t the best UI for this. Maybe I should follow the UI the NuGet team came up with. I don’t know. So feel free to suggest some design improvements if you have any ideas. Better yet, this is open source, feel free to submit a PR.
Let’s Wrap this Baby Up!
I hope you find these snippets, item templates, project templates, and custom editors useful. If you have any ideas or suggestions, please feel free to send them my way. Also, keep in mind that this was my first attempt at creating templates and a custom editor, so if you find any issues be gentle. I am learning as I go and could really use your help to improve them. Be sure to report any issues you run into, and feel free to fix them and submit your PR.