Catel 3.4 is released!

by Geert 15. December 2012 16:43

It has been quite a while, but today a new version of Catel is released. It took us more time then usual, but in return we have a great version ready to be downloaded via the site or via NuGet.

Added WinRT and Windows Phone 8 support

This is probably the most noticeable “feature” of all. Both WinRT and Windows Phone 8 are fully supported with all features that you are familiar with in Catel. This means that we were able to port everything (not really everything, but 90 %) of it to WinRT and all known Windows Phone features to Windows Phone 8.

Performance improvements

For this release, we focused on performance. We made a lot of improvements by tracing and tweaking all the calls thoroughly. Below is a list of tweaks:

  • All reflection calls are now being cached
  • Logging is only executed when there are actually listeners registered
  • ModelBase.LeanAndMeanModel property to (temporarily) suspend both validation and change notifications, great when restoring models from disk
  • Added a FastObservableCollection which allows the adding of a range and only invokes a single event at the end
  • It is now possible to control (globally or per control) whether InfoBarMessageControl and WarningAndErrorValidator controls must be created or MVVM controls
  • Constructors in the ServiceLocator are cached which gives a major performance boost when creating new instances of types with dependency injection

Improved IoC container

The IoC container in Catel, the ServiceLocator, now supports registration of types with a tag. This allows the same type to be registered multiple times.

And much more…

Besides new features, we also fixed over 40 issues in this new release. Not all new features are described in this blog post, so make sure you get the latest version while it’s hot!

Tags:

MVVM | C# | Catel | Windows Phone 7 | Windows Phone 8 | WinRT | WPF | Silverlight

Showing a busy indicator per view in Silverlight using MVVM

by Geert 12. October 2012 10:03

Introduction

When using Silverlight, everything is asynchronous. It seems to be the trend, the same goes for Windows 8. This means that you will need to inform the user about progress in the background.

Silverlight uses the BusyIndicator for this behavior. Using MVVM, it might be a bit harder to implement the BusyIndicator in a correct way, but Catel provides the IPleaseWaitService for years which can be mocked easily during test scenarios.

However, it didn’t support a busy indicator per view yet. This fact has changed today, as you can see in the screenshot below:

image

A long requested feature in Catel was the support for tags in the ServiceLocator. The ServiceLocator is the IoC solution that Catel provides by default. A customer of Catel recently required the busy indicators to show up per view. I thought a bit about it and this could be solved by the recently (read: this week) introduction of the tags in the ServiceLocator.

Setting up the views

The view is responsible for registering the service. This can easily be done using the Catel user controls. Create a view like you always do, then use the following code-behind:

   1:  protected override void OnViewModelChanged()
   2:  {
   3:      var serviceLocator = ServiceLocator.Default;
   4:   
   5:      var viewModel = ViewModel;
   6:      if (viewModel != null)
   7:      {
   8:          serviceLocator.RegisterInstance(typeof (IPleaseWaitService), new PleaseWaitService(this), viewModel);
   9:      }
  10:  }

This code will be executed when the ViewModel property of the control changes. The view registers a view specific instance of the PleaseWaitService service in the ServiceLocator. It uses the new view model as tag so that will be used to distinguish the services.

Setting up the view models

The view model can retrieve the PleaseWaitService very easily because the tag is itself. To show the view specific please wait service, use this code:

   1:  var pleaseWaitService = GetService<IPleaseWaitService>(this);
   2:  pleaseWaitService.Show();

To hide the window again, use this code:

   1:  var pleaseWaitService = GetService<IPleaseWaitService>(this);
   2:  pleaseWaitService.Hide();

 

Customizing the PleaseWaitService

Customizing the please wait service is very, very easy. Just override the class like this:

   1:  public class MyCustomPleaseWaitService : PleaseWaitService
   2:  {
   3:      protected override FrameworkElement CreateBusyIndicator()
   4:      {
   5:          var busyIndicator = new MyBusyIndicatorControl();
   6:          
   7:          busyIndicator.SetBinding(System.Windows.Controls.BusyIndicator.BusyContentProperty, new Binding("Status"));
   8:          busyIndicator.SetBinding(System.Windows.Controls.BusyIndicator.IsBusyProperty, new Binding("IsBusy"));
   9:          
  10:          return busyIndicator();
  11:      }
  12:  }

The base implementation will automatically take care that the data context is updated and that the control is centered as required.

Demo and source

Last but not least, here is a demo and the source.

PleaseWaitServicePerView.zip (21.24 kb) [Downloads: 459]

Tags:

C# | Catel | MVVM | Silverlight

Catel 3.2 is released!

by Geert 8. July 2012 11:32

The Catel team is very proud the announce the new 3.2 release. As always, the new version is available via NuGet and via Codeplex (http://catel.codeplex.com).

In this blog post, I will try to explain the most important new changes in Catel.

Dependency injection support in the ServiceLocator

The ServiceLocator in Catel now fully supports dependency injection. This means that when a type is being resolved from the ServiceLocator.

Dependency injection via the ServiceLocator in Catel is enabled by default. This means that when a type is resolved from the container, it will automatically use dependency injection to construct the type if it is not registered as instance.

It will first search for all available constructors on the type that will be instantiated. Then, for each constructor, starting with the one with the most parameters, it will try to retrieve all values. If one fails, it will go to the next. If all fail, it will try to use the default constructor without parameters. If that fails as well, then the type cannot be constructed and an exception will be thrown.

To get a better understanding of what happens, see the class below:

public class MyClass
{
  private IFirstDependency _firstDependency;
  private ISecondDependency _secondDependency;
 
  public MyClass()
     : this(null) { }
 
  public MyClass(IFirstDependency firstDependency)
     : this(firstDependency, null) { }
 
  public MyClass(IFirstDependency firstDependency, ISecondDependency secondDepdenceny)
  {
    _firstDependency = firstDependency;
    _secondDependency = secondDependency;
  }
}

When the MyClass will be retrieved from the ServiceLocator, this will happen:

  1. Find constructor with most parameters (the one with both firstDependency and secondDependency). If both IFirstDependency and ISecondDependency can be resolved from the ServiceLocator, the type will be constructed with the constructor. Otherwise it will proceed with step 2.
  2. Find next constructor with most parameters (the one with only firstDependency). If IFirstDependency can be resolved from the ServiceLocator, the type will be constructed with the constructor. Otherwise it will proceed with step 3.
  3. At this point, no constructor could be used. In this case, the ServiceLocator will try to use the default constructor (the one without parameters) as last resort to instantiate the type.

Introducing the ViewModelFactory

In previous versions of Catel, it was only possible to inject a datacontext into a view model. In Catel 3.2, the new ViewModelFactory is introduced. This class is responsible for instantiating the view model.

By default, the implementation does exactly the same as before, but this gives great freedom when the view models cannot be instantiated by Catel itself (for example, when dependency injection is used, or when the registered view models of a control are interfaces).

Below is an example of a custom implementation of a view model factory:

public class CustomViewModelFactory : ViewModelFactory
{
  public override IViewModel CreateViewModel(Type viewModelType, object dataContext)
  {
      if (viewModelType == typeof(MySpecialViewModel))
      {
          // Use custom constructor with dependency injection
          return new MySpecialViewModel(dep1, dep2, dep3, dataContext);
      }
 
      // Fall back to default behavior
      return base.CreateViewModel(viewModelType, dataContext);
  }
}

All reflection is cached

In preparation for WinRT support, we moved all reflection to a single class with extension methods. This was a great opportunity to add caching to all reflection.

This means that all reflection is cached on the first call. This provides major performance improvements when using reflection.

More flexible behavior base classes

Catel already provided memory leak free base classes for behaviors and triggers. In the new version, a few new base classes are introduced to allow faster behavior developmentP:

  • CommandBehaviorBase – supports a command, command parameter and key modifiers out of the box
  • UpdateBindingBehaviorBase – supports binding updates on dynamic dependency properties

Core services now only use interfaces for MVVM integration

The core services (such as the MVVM behaviors) in Catel now use interfaces such as INotifyableViewModel and IRelationViewmodel. This makes it possible to use different base classes for view models and still use the full potential of Catel.

Updated to latest external libraries

We also took the time to update all external libraries to the latest version. The following libraries were updated:

  • Ninject (3.0)
  • Castle.Windsor (3.0)
  • FluentValidation (3.3.1)

Tags: ,

C# | Catel | MVVM | WPF | Windows Phone 7 | Silverlight

Catel 3.1 released

by Geert 6. May 2012 13:49

We just released Catel 3.1. You can grab the latest version:

In this blog post, we already described the most important changes, but you can find a full list of changes here.

As always, if you have any questions or feature requests, don’t hesitate to contact us or create an issue!

Tags: ,

C# | Catel | MVVM | Silverlight | Windows Phone 7 | WPF

Catel 3.1 - beta version released

by Geert 27. April 2012 18:25

So, it’s been another month since we released Catel 3.0. This means that soon, we will release a new version, and it will be 3.1. You can grab the latest version:

Below is a list of the most important changes.

OnDataContextChanged and OnPropertyChanged methods

Since lots of people need to write handlers (but forget to unsubscribe thus cause memory leaks), we made these available as default to all controls that Catel provides.

AsynchronousCommand

Asynchronous development becomes more and more important. In the latest version, we proudly introduce the AsynchronousCommand, which is a command that runs in the background and doesn’t block and UI. It is able to provide thread-safe callbacks to the UI as well.

You can read all about it in the official docs.

Memento pattern

The memento pattern is something hardly any custom made application implements due to its complexity. Catel now includes a memento service which allows to undo/redo changes on properties, collections and custom actions.

If you ever need to build in undo/redo support, this is what you are looking for! You can read all about it in the official docs.

Note that this feature is also available for non-xaml solutions

Improved handling of datacontext changes for nested user control scenarios

If you are using Catel, you are probably aware that you sometimes get false reports of binding errors. Catel now uses an improved way of determining the right datacontext in nested user control scenarios so these false errors no long appear in your output window.

What to expect next?

We are currently investigating what it takes to port Catel to WinRT. However, since reflection in WinRT is very basic (for example, we cannot get the name of a method), it is really hard to implement the WeakEventListener and MessageMediator in WinRT. However, we will keep trying…

You can also checkout the road map.

Tags: ,

C# | Catel | MVVM | WPF | Silverlight | NuGet | Windows Phone 7


About the Author

Geert van Horrik is an independent freelance software developer since January 1st, 2007. Since then he was been working on several projects from C++ to C# (WPF, Silverlight, ASP.NET, etc). Currently he loves to write his software using WPF (or Silverlight if WPF isn't an option).

Lately, Geert is spending a lot of time on Catel, a free open-source MVVM Framework for WPF and Silverlight. Actually, it's more than "just" an MVVM Framework, it's a complete application library!