In the spirit of continuing to build up set of project templates for F#, I've created an F# Silverlight MVVM multi-project template.
Here are the links to the other templates that have been announced on this blog:
- WPF MVVM Multi-Project Template: A Polyglot Approach
- An F# WPF MVVM Project Template
- Standard WCF Template
- Standard ASP.NET MVC 2 Template
The code provided by this multi-project template creates an application that is very similar to the output of the previously metioned F# WPF templates.
It looks something like this:
The following list shows the high level code changes that were needed to port the polyglot WPF template to Silverlight:
- Created new projects from the default C# and F# Silverlight 4 project templates.
- Made several revisions related to XAML resources.
- Changed the DataGrid control and Label controls to the Silverlight versions.
You can download the template here and find the full source at http://github.com/dmohl/FSharpSilverlightMVVMTemplate.
Showing posts with label Template. Show all posts
Showing posts with label Template. Show all posts
Wednesday, July 28, 2010
Monday, June 21, 2010
An F# WPF MVVM Project Template
I've been planning for a while to create an F# WPF MVVM Template to add to the other templates that have been announced on this blog. A resent post by Mark Pearl provided a great simple example which helped kick me into gear and bring this plan to fruition.
To get us up to date, here are the links to the other templates that I have created:
- Standard WCF Template
- Standard ASP.NET MVC 2 Template
This particular template is slightly different than the others. While the others had most or all of the code written in F#, the views or endpoints were still provided via a C# project. In contrast, this F# WPF MVVM template contains only F# projects.
What to Expect:
The code provided by this template creates an application that is loosely based on ExpenseIt (A simple expense report app. defined on this MSDN page). The following screenshot displays the produced application in action:
The Model:
The model for this application is comprised of two simple records:
The view models are fairly standard. Each view model inherits from a ViewModelBase class. The ExpenseItHomeViewModel class contains most of the code. Since these are the two most interesting classes associated with view models, they will be the only two shown.
ViewModelBase.fs
The views are similar to views used in any WPF MVVM application. The solution has three XAML files: ApplicationResources.xaml, MainWindow.xaml, and ExpenseItHome.xaml. Since ExpenseItHome.xaml is the most interesting of these three, it is provided below:
To get us up to date, here are the links to the other templates that I have created:
- Standard WCF Template
- Standard ASP.NET MVC 2 Template
This particular template is slightly different than the others. While the others had most or all of the code written in F#, the views or endpoints were still provided via a C# project. In contrast, this F# WPF MVVM template contains only F# projects.
What to Expect:
The code provided by this template creates an application that is loosely based on ExpenseIt (A simple expense report app. defined on this MSDN page). The following screenshot displays the produced application in action:
The Model:
The model for this application is comprised of two simple records:
type Expense =
{ ExpenseType : string
ExpenseAmount : string}
type ExpenseReport =
{ Name : string
Department : string
ExpenseLineItems : seq<expense>}
The View Models:
The view models are fairly standard. Each view model inherits from a ViewModelBase class. The ExpenseItHomeViewModel class contains most of the code. Since these are the two most interesting classes associated with view models, they will be the only two shown.
ViewModelBase.fs
namespace FSharpWpfMvvmTemplate.ViewModel
open System
open System.Windows
open System.Windows.Input
open System.ComponentModel
type ViewModelBase() =
let propertyChangedEvent = new DelegateEvent<PropertyChangedEventHandler>()
interface INotifyPropertyChanged with
[<CLIEvent>]
member x.PropertyChanged = propertyChangedEvent.Publish
member x.OnPropertyChanged propertyName =
propertyChangedEvent.Trigger([| x; new PropertyChangedEventArgs(propertyName) |])
ExpenseItHomeViewModel.fs
namespace FSharpWpfMvvmTemplate.ViewModel
open System
open System.Xml
open System.Windows
open System.Windows.Data
open System.Windows.Input
open System.ComponentModel
open System.Collections.ObjectModel
open FSharpWpfMvvmTemplate.Model
type ExpenseItHomeViewModel =
[<DefaultValue(false)>] val mutable _collectionView : ICollectionView
[<DefaultValue(false)>] val mutable _expenseReports : ObservableCollection<ExpenseReport>
new () as x = {_expenseReports = new ObservableCollection<ExpenseReport>();
_collectionView = null} then x.Initialize()
inherit ViewModelBase
member x.Initialize() =
x._expenseReports <- x.BuildExpenseReports()
x._collectionView <- CollectionViewSource.GetDefaultView(x._expenseReports)
x._collectionView.CurrentChanged.AddHandler(
new EventHandler(fun s e -> x.OnPropertyChanged "SelectedExpenseReport"))
member x.BuildExpenseReports() =
let collection = new ObservableCollection<ExpenseReport>()
let mike = {Name="Mike"
Department="Legal"
ExpenseLineItems =
[{ExpenseType="Lunch"
ExpenseAmount="50"};
{ExpenseType="Transportation"
ExpenseAmount="50"}]}
collection.Add(mike)
let lisa = {Name="Lisa"
Department="Marketing"
ExpenseLineItems =
[{ExpenseType="Document printing"
ExpenseAmount="50"};
{ExpenseType="Gift"
ExpenseAmount="125"}]}
collection.Add(lisa)
let john = {Name="John"
Department="Engineering"
ExpenseLineItems =
[{ExpenseType="Magazine subscription"
ExpenseAmount="50"};
{ExpenseType="New machine"
ExpenseAmount="600"};
{ExpenseType="Software"
ExpenseAmount="500"}]}
collection.Add(john)
let mary = {Name="Mary"
Department="Finance"
ExpenseLineItems =
[{ExpenseType="Dinner"
ExpenseAmount="100"}]}
collection.Add(mary)
collection
member x.ExpenseReports : ObservableCollection<ExpenseReport> = x._expenseReports
member x.ApproveExpenseReportCommand =
new RelayCommand ((fun canExecute -> true), (fun action -> x.ApproveExpenseReport))
member x.SelectedExpenseReport =
x._collectionView.CurrentItem :?> ExpenseReport
member x.ApproveExpenseReport =
MessageBox.Show(sprintf "Expense report approved for %s" x.SelectedExpenseReport.Name) |> ignore
The Views:The views are similar to views used in any WPF MVVM application. The solution has three XAML files: ApplicationResources.xaml, MainWindow.xaml, and ExpenseItHome.xaml. Since ExpenseItHome.xaml is the most interesting of these three, it is provided below:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:ViewModel="clr-namespace:FSharpWpfMvvmTemplate.ViewModel;assembly=FSharpWpfMvvmTemplate.ViewModel" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignWidth="424">
<UserControl.DataContext>
<ViewModel:ExpenseItHomeViewModel></ViewModel:ExpenseItHomeViewModel>
</UserControl.DataContext>
<UserControl.Resources>
<ResourceDictionary Source="ApplicationResources.xaml" />
</UserControl.Resources>
<Grid Margin="10,0,10,10" VerticalAlignment="Stretch">
<Grid.Resources>
<!-- Name item template -->
<DataTemplate x:Key="nameItemTemplate">
<Label Content="{Binding Path=Name}"/>
</DataTemplate>
<!-- Expense Type template -->
<DataTemplate x:Key="typeItemTemplate">
<Label Content="{Binding Path=ExpenseType}"/>
</DataTemplate>
<!-- Amount item template -->
<DataTemplate x:Key="amountItemTemplate">
<Label Content="{Binding Path=ExpenseAmount}"/>
</DataTemplate>
</Grid.Resources>
<Grid.Background>
<ImageBrush ImageSource="watermark.png" />
</Grid.Background>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!-- People list -->
<Label Grid.Row="0" Grid.ColumnSpan="2" Style="{StaticResource headerTextStyle}" >
View Expense Report
</Label>
<Grid Margin="10" Grid.Column="0" Grid.Row="1" VerticalAlignment="Top">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Border Grid.Row="1" Style="{StaticResource listHeaderStyle}">
<Label Style="{StaticResource listHeaderTextStyle}">Names</Label>
</Border>
<ListBox Name="peopleListBox" Grid.Row="2"
ItemsSource="{Binding Path=ExpenseReports}"
ItemTemplate="{StaticResource nameItemTemplate}"
IsSynchronizedWithCurrentItem="True">
</ListBox>
<!-- View report button -->
</Grid>
<Grid Margin="10" Grid.Column="1" Grid.Row="1" DataContext="{Binding SelectedExpenseReport}" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="57*" />
<ColumnDefinition Width="125*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Name -->
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="0" Orientation="Horizontal">
<Label Style="{StaticResource labelStyle}">Name:</Label>
<Label Style="{StaticResource labelStyle}" Content="{Binding Path=Name}"></Label>
</StackPanel>
<!-- Department -->
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Orientation="Horizontal">
<Label Style="{StaticResource labelStyle}">Department:</Label>
<Label Style="{StaticResource labelStyle}" Content="{Binding Path=Department}"></Label>
</StackPanel>
<Grid Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Top" HorizontalAlignment="Left">
<!-- Expense type and Amount table -->
<DataGrid ItemsSource="{Binding Path=ExpenseLineItems}" ColumnHeaderStyle="{StaticResource columnHeaderStyle}" AutoGenerateColumns="False" RowHeaderWidth="0" Margin="0,0,-139,0">
<DataGrid.Columns>
<DataGridTextColumn Header="ExpenseType" Binding="{Binding Path=ExpenseType}" />
<DataGridTextColumn Header="Amount" Binding="{Binding Path=ExpenseAmount}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
<Button Grid.Row="2" Command="{Binding ApproveExpenseReportCommand}" Style="{StaticResource buttonStyle}" Grid.Column="1" Margin="0,10,53,0">Approve</Button>
</Grid>
</UserControl>
Conclusion:
You can download the template installer here and find the full source at http://github.com/dmohl/FSharpWpfMvvmTemplate. I did run into a few limitations with having the views in an F# project. Because of these limitations, I would likely use a polyglot approach with a C# project as the view container and F# projects for the model and view model containers for solutions that are any more complex than this example. I plan to provide a template for the polyglot approach in my next blog post.
Thursday, May 20, 2010
Creating an F# Solution Template for Visual Studio 2010
The title of this post is misleading because you can't actually create a VS2010 solution template (to my knowledge at least). However, you can create a multi-project template and when I first went looking for how to do this, a search for "how to create a VS2010 solution template" was the first thing that came to mind (apparently many others have done this as well). Hopefully, this post will make it easier for others to find the information for which they are searching.
In my last post, I provided an F# ASP.NET MVC 2 Web Application template that provides the same functionality as the out of the box C# ASP.NET MVC 2 Web Application template. In this post, I will show how that template was created. The full solution including all *.vstemplate and *.vscontent files can be found at http://github.com/dmohl/FSharpMVC2Starter.
Creating the *.vstemplate File for Each Project:
The first step in creating a multi-project template is to create a *.vstemplate file for each of the projects that you wish to include. This can be accomplished using the export template wizard in visual studio (view the steps on the MSDN web site) or by manually creating the template (view the steps on the MSDN website).
This is what my *.vstemplate file looks like for the FSharpMVC2.Controllers project:
Creating the Root *.vstemplate File:
Once the *.vstemplate files have been created, it's time to create a root *.vstemplate. This looks very similar to the project level *.vstemplate files; however, it references each project level *.vstemplate file rather than the specific project level source/content files.
My root level *.vstemplate file looks like this:
Compresssing the Files:
Once all of the *.vstemplate files are created, it is time to compress all files that are to be included in the template. To do this, select all the desired items including the root *.vstemplate file, then right-click and choose Send To/Compressed (zipped) Folder. This will create a single *.zip file.
Creating the Template Installer:
We are now ready to create our template installer. This involves the creation of one last XML file named *.vscontent. My FSharpMVC2Template.vscontent file looks like this:
Once the *.vscontent file has been created, select it, and the previously created zip file, and compress them into a single zip file. Change the extension of this file to .vsi and you're ready for a test run.
In my last post, I provided an F# ASP.NET MVC 2 Web Application template that provides the same functionality as the out of the box C# ASP.NET MVC 2 Web Application template. In this post, I will show how that template was created. The full solution including all *.vstemplate and *.vscontent files can be found at http://github.com/dmohl/FSharpMVC2Starter.
Creating the *.vstemplate File for Each Project:
The first step in creating a multi-project template is to create a *.vstemplate file for each of the projects that you wish to include. This can be accomplished using the export template wizard in visual studio (view the steps on the MSDN web site) or by manually creating the template (view the steps on the MSDN website).
This is what my *.vstemplate file looks like for the FSharpMVC2.Controllers project:
<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
<TemplateData>
<Name>FSharpMVC2.Controllers</Name>
<Description>MVC Controllers in F#</Description>
<ProjectType>FSharp</ProjectType>
<ProjectSubType>
</ProjectSubType>
<SortOrder>1000</SortOrder>
<CreateNewFolder>true</CreateNewFolder>
<DefaultName>FSharpMVC2.Controllers</DefaultName>
<ProvideDefaultName>true</ProvideDefaultName>
<LocationField>Enabled</LocationField>
<EnableLocationBrowseButton>true</EnableLocationBrowseButton>
<Icon>__TemplateIcon.ico</Icon>
</TemplateData>
<TemplateContent>
<Project TargetFileName="FSharpMVC2.Controllers.fsproj" File="FSharpMVC2.Controllers.fsproj" ReplaceParameters="true">
<ProjectItem ReplaceParameters="false" TargetFileName="HomeController.fs">HomeController.fs</ProjectItem>
<ProjectItem ReplaceParameters="false" TargetFileName="AccountController.fs">AccountController.fs</ProjectItem>
</Project>
</TemplateContent>
</VSTemplate>
The ProjectType element value of "FSharp" specifies that this is an F# template.
Creating the Root *.vstemplate File:
Once the *.vstemplate files have been created, it's time to create a root *.vstemplate. This looks very similar to the project level *.vstemplate files; however, it references each project level *.vstemplate file rather than the specific project level source/content files.
My root level *.vstemplate file looks like this:
<VSTemplate Version="2.0.0" Type="ProjectGroup"
xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
<TemplateData>
<Name>F# ASP.NET MVC2 Web Application</Name>
<Description>F# ASP.NET MVC2 Web Application</Description>
<Icon>__TemplateIcon.ico</Icon>
<ProjectType>FSharp</ProjectType>
</TemplateData>
<TemplateContent>
<ProjectCollection>
<ProjectTemplateLink ProjectName="FSharpMVC2.Controllers">
src\FSharpMVC2.Controllers\MyTemplate.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="FSharpMVC2.Models">
src\FSharpMVC2.Models\MyTemplate.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="FSharpMVC2.Core">
src\FSharpMVC2.Core\MyTemplate.vstemplate
</ProjectTemplateLink>
<ProjectTemplateLink ProjectName="FSharpMVC2.Web">
src\FSharpMVC2.Web\MyTemplate.vstemplate
</ProjectTemplateLink>
</ProjectCollection>
</TemplateContent>
</VSTemplate>
Once again, the ProjectType element value of "FSharp" indicates that this is an F# template.Compresssing the Files:
Once all of the *.vstemplate files are created, it is time to compress all files that are to be included in the template. To do this, select all the desired items including the root *.vstemplate file, then right-click and choose Send To/Compressed (zipped) Folder. This will create a single *.zip file.
Creating the Template Installer:
We are now ready to create our template installer. This involves the creation of one last XML file named *.vscontent. My FSharpMVC2Template.vscontent file looks like this:
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>FSharpMVC2Template.zip</FileName>
<DisplayName>F# ASP.NET MVC2 Web Application</DisplayName>
<Description>F# ASP.NET MVC2 Web Application</Description>
<FileContentType>VSTemplate</FileContentType>
<ContentVersion>1.0</ContentVersion>
<Attributes>
<Attribute name="ProjectType" value="Visual Web Developer"/>
<Attribute name="ProjectSubType" value=""/>
<Attribute name="TemplateType" value="Project"/>
</Attributes>
</Content>
</VSContent>
Note: Make sure that the FileName element value matches the name of the compressed file that you made during the "Compressing the Files" step.Once the *.vscontent file has been created, select it, and the previously created zip file, and compress them into a single zip file. Change the extension of this file to .vsi and you're ready for a test run.
Subscribe to:
Posts (Atom)

