Archive for the ‘General’ category
Final reminder: 2 days left on our January offer!
Tagged as GeneralJust a quick note: If you’ve seen our big January 2012 offer where you save hundreds of dollars and get one of the best .NET suites around.
5 reasons to take a look
1. Get 9 high quality .NET products.
2. Save at least $300 USD.
3. Get 12 months of new releases.
4. Get 12 months of nightly builds.
5. Get any new product Mindscape releases in the next 12 months.
Taking all that into account and considering that our Mindscape Mega Pack is already very competitively priced, you’d be crazy not to take up this offer before time runs out! The offer extends to team licenses and site licenses too.
This offer strictly ends on the 1st of February.
Caliburn Micro Part 2: Data Binding and Events
In my previous blog post I showed you how to get started with using the Caliburn Micro Framework in a WPF application. Caliburn Micro helps us implement the application using the MVVM design pattern to get a clean separation between the view and the data model. In this blog post we’ll take a look at how Caliburn Micro assists us with data binding and events. We will be building on top of the application outlined in the previous blog post to add some simple user interaction and data display.
Data binding
We’ll start by getting the application to display a numerical value that is stored in the model. In the AppViewModel class created in the previous blog post, add a property called Count as seen in the code snippet below. The property value is stored in the _count field which we have given a default value of 50. You may recall from last time that we made the AppViewModel class extend the PropertyChangedBase provided by Caliburn Micro to preform property change notifications. Rather than implementing INotifyPropertyChanged in all of your models, you can simply call the NotifyOfPropertyChange method within the setter of your properties.
public class AppViewModel : PropertyChangedBase { private int _count = 50; public int Count { get { return _count; } set { _count = value; NotifyOfPropertyChange(() => Count); } } }
Next we modify the view to display this property value. In AppView.xaml, add a TextBox to the grid as follows:
<Grid MinWidth="300" MinHeight="300" Background="LightBlue"> <TextBlock Name="Count" Margin="20" FontSize="150" VerticalAlignment="Center" HorizontalAlignment="Center" /> </Grid>
Now run up the application and see that the TextBlock is displaying the default value of the Count property.
Wait… what? I don’t remember setting any binding on the TextBlock to get the Count property, and yet the TextBlock is displaying the correct data!
Notice that I’ve set the name of the TextBlock to be the same as the property we want to bind to. This is a convenient short cut that Caliburn Micro provides. For elements that display data such as TextBlock or TextBox, setting their name to match a property on the data model will automatically hook it up for you.
Handling events
Next, let’s work on adding a button that increments the displayed value. The quick and dirty way of doing this is to hook up the Click event of a button to an event handler in the code behind. However, when using the MVVM pattern it’s usually best (but not absolutely necessary) to avoid using the code behind where you can. So let’s look at how to handle the events Caliburn Micro style. First, add a method to the AppViewModel for incrementing the Count property like this:
public void IncrementCount() { Count++; }
Now add a button to the grid in AppView.xaml as follows:
<Grid MinWidth="300" MinHeight="300" Background="LightBlue"> <RepeatButton Name="IncrementCount" Content="Up" Margin="15" VerticalAlignment="Top" /> <TextBlock Name="Count" FontSize="150" VerticalAlignment="Center" HorizontalAlignment="Center" /> </Grid>
Running the application now and clicking the button will increment the count value as advertised. Once again you’ll notice that we don’t need to do much work to hook the click event of the button to the IncrementCount method. For certain user interface controls such as buttons, you can simply set the name of the control to be the name of the method you want it to be hooked to. Caliburn Micro will hook the appropriate event of the user control to the specified method in the model. In the case of buttons, Caliburn Micro deals with the Click event. (You can also manually specifying which event to hook up to which method which we’ll look at next time).
Event guards
When Caliburn Micro automatically hooks up the Click event to the IncrementCount method, it also looks for a method or property called CanIncrementCount. By adding a CanIncrementCount method or property, you can include additional logic that determines whether the event is allowed to be handled based on the current state of the model. Let’s do this now by adding the following property to the AppViewModel:
public bool CanIncrementCount { get { return Count < 100; } }
Since this logic is based on the value of the Count property, we also need to raise property change notification for the CanIncrementCount property whenever the Count value changes. This is done by adding this line of code on the Count property setter:
NotifyOfPropertyChange(() => CanIncrementCount);
Run up the application once more and increment the value to 100. Once the limit has been reached, the button will become disabled and prevent the user from further incrementing the value.
In this tutorial we have looked at a few ways that Caliburn Micro takes some of the work off our shoulders. Now you can more quickly use data binding, property change notifications, event handlers and event guards, while at the same time implementing a sturdy MVVM application that is easier to test and maintain.
You can download the full Visual Studio 2010 solution for this tutorial here. Use it to practice your new skills by adding a button to decrement the value.
In the next instalment of this blog series, we’ll take a look at more advanced scenarios of event handling with Caliburn Micro, specifically with passing event parameters. See you then.
Caliburn Micro Part 1: Getting Started
A few days ago I blogged about our new Dashboard sample included in WPF Elements. The sample demonstrates how to create a sales dashboard using a few of our WPF controls like the Data Grid, Time Explorer and Charts. It was built with the help of the Caliburn Micro framework to give it a robust MVVM architecture. In this blog post I’ll walk you through a simple tutorial for getting started with using Caliburn Micro in a WPF application. This is just part 1 in a series I will be writing so I hope you’ll subscribe and follow along with me as we build up to a useful application.
Step 1: Getting Started
Caliburn Micro targets .NET framework 4.0, so you’ll need Visual Studio 2010 to create the application. Start by creating a new WPF Application and add a reference to Caliburn.Micro.dll and System.Windows.Interactivity.dll which come with the Caliburn Micro download. Since Caliburn Micro is going to take care of creating the window for you, delete MainWindow.xaml and remove the StartupUri attribute from App.xaml. App.xaml will now look like this:
<Application x:Class="CaliburnMicroApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> </Application.Resources> </Application>
Step 2: The View Model
Caliburn Micro promotes a View-Model-First approach, so the next step is to add a class to represent the view model. Large applications can have many view models, each of which provides the logic for a different view. Below is a code example of an empty view model. Since this tutorial is focused on simply getting started with incorporating Caliburn Micro into a WPF application, we are not going to make the view model do anything for now.
using Caliburn.Micro; namespace CaliburnMicroApp { public class AppViewModel : PropertyChangedBase { } }
The first thing to notice here is the name of the class. Caliburn Micro expects a particular naming convention so it can hook up the view model to the appropriate view. The class name of a view model should end with “ViewModel”. What you put in front of “ViewModel” is up to you. The other thing to notice here is that this class extends the PropertyChangedBase. This is provided by Caliburn Micro and makes it easy to raise property change notifications without needing to implement INotifyPropertyChanged in all your view models. Although this example view model doesn’t do anything, I’ve included the PropertyChangedBase as good practice. When adding properties to the view model, it will come in handy.
Step 3: The View
In order to display something in the window, you’ll need to create a view for the view model created in the previous step. This is as simple as adding a new UserControl to the project as seen below. I should point out again that Caliburn Micro expects a particular naming convention so it can hook up the view to the appropriate view model. View names should end with “View” and start with the same name you used for the view model. So for my example here, “AppView” is the view for rendering the “AppViewModel”. In the code below I have also set the width, height and background of the grid so that when you run this application you can see that it is working correctly.
<UserControl x:Class="CaliburnMicroApp.AppView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> <Grid Width="300" Height="300" Background="LightBlue"> </Grid> </UserControl>
Step 4: The Bootstrapper
The bootstrapper is the mechanism used to incorporate Caliburn Micro into your application. It is also a place where you can configure the framework for the needs of your application. For the purposes of this tutorial, I have used a very simple bootstrapper implementation seen here:
using Caliburn.Micro; namespace CaliburnMicroApp { public class AppBootstrapper : Bootstrapper<AppViewModel> { } }
Caliburn Micro has 2 different bootstrappers available. The one used above lets you set the generic type to be the view model that you want to be used at startup. The last step is to tell the application to use the bootstrapper. This is done by adding your bootstrapper to a resource dictionary in App.xaml. After doing this, App.xaml will now look something like this:
<Application x:Class="CaliburnMicroApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:CaliburnMicroApp" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary> <local:AppBootstrapper x:Key="bootstrapper" /> </ResourceDictionary> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>
And that’s it
Now when you run up the application, you’ll see a small window with a light blue background. Your application is now primed and ready to take advantage of all the great support provided by Caliburn Micro. It’s easy to get help for using this framework due to its growing community. You can also look through the documentation on CodePlex.
You can grab the full Visual Studio 2010 solution I made for this tutorial here.
In the next part of this blog series we will look at adding some interactivity using events and data binding with the help of Caliburn Micro.
Until next time, Happy Coding!
LightSpeed Book – now available!
I’m pleased to announce that LightSpeed developers everywhere can now purchase the official LightSpeed User Guide in printed form! Over 330 pages of useful advice and guidance on working with LightSpeed. The user guide acts as a great reference for your team and you will be sure to discover some gems to help improve your development experience even more.
Some of the topics covered:
- Creating Domain Models
- Basic operations with LightSpeed
- Building web applications with LightSpeed
- Controlling database mapping
- Working with entities
- Advanced querying
- Search engine indexing
- Much more
Pricing and availability
The eBook version is free – no charge. The physical version of the book is priced at $14.99 USD which covers production costs.
You can obtain either version from the Mindscape Lulu page.
Action shot
The summer sale is here!
Tagged as General, LightSpeed, MegaPack, NHibernate Designer, Phone Elements, Products, SharePoint, Silverlight Elements, SimpleDB Management Tools, WPF, WPF Diagrams, WPF Elements, WPF Property Grid
Note: This sale has now ended.
For our friends in the northern hemisphere it’s that time of the year – it’s warming up and there’s a holiday approaching. We thought it would be a great time to offer a special for the summer so that you have a great toolbox at your disposal for when you’re coding away on the beach.
Here’s the deal:
For the price of our WPF or Silverlight suites you can get a full Mindscape Mega Pack upgrade at no extra charge! And to save you money in the long run we’ll also add an additional six months of updates! Now that will afford you a few extra Mojitos!
So that’s every Mindscape product — WPF, Silverlight, Windows Phone, LightSpeed, SimpleDB, you name it — and 18 whole months of nightly builds, upgrades and entirely new products! All at a fraction of the price of other competing suites.
Throw on your skate shoes and roll over to the Summer Sale Page!
Categories
BrainDump (1)
Community Code (4)
Events (15)
F# (11)
General (50)
Lab Samples (2)
LightSpeed (249)
MegaPack (7)
News (68)
NHibernate Designer (18)
Nightly news (40)
Phone Elements (22)
Products (87)
Projects (5)
Screencast (6)
SharePoint (3)
Silverlight (14)
Silverlight Elements (59)
SimpleDB Management Tools (20)
Visual Studio (9)
VS File Explorer (7)
Web Workbench (20)
WPF (43)
WPF Diagrams (53)
WPF Elements (91)
WPF Property Grid (32)



Posted by John-Daniel Trask on 29 January 2012 






