Full Disclosure: The implementation presented in this post is experiential. While it passes the PDFx's Unit Tests successfully, it has not undergone several months of stress tests in IK's production environments (in contrast to the PDFx itself) because we're using our own in house ViewModel base class and are consequently able to derive from BindableExt.

Using PDFx with MVVM Light or a similar framework

In Using PDFx in your Project you've learned what steps you have to take in order to use the PDFx in your own project. As it was explained, every class that wants to leverage PDFx's features needs to ultimately derive from PDFx's Bindable or BindableExt because all the functionality is exposed through protected methods.

In certain scenarios, however, the ViewModel Base class that you're using throughout the project might live in a 3rd party assembly and you might consequently not have the option to modify its source code to include the PDFx.

In order to still support those scenarios, I've extended the PDFx's base classes to expose their functionality not only through protected methods but also through explicitly implemented interfaces. This allows you to use the PDFx in your project by implementing an intermediate man-in-the-middle ViewModel class that bridges the gap between your ViewModel base class and the PDFx. The following steps are necessary to implement such an intermediate ViewModel (You can skip the following steps and immediately head to the next section of this post if you would like to reuse my implementation):

Let's assume every ViewModel in your project derives from ViewModelBase that is exposed by a 3rd party library.

  1. Create a new class MyViewModel and derive it from ViewModelBase.
  2. Change every reference in your project from ViewModelBase to MyViewModel.
  3. Create a nested BindableExt or Bindable instance inside of MyViewModel (See Library Versions to find out which one suits your needs best)
    1. If you use Bindable, create delegating methods within MyViewModel for every method the Bindable instance exposes through its explicitly implemented interface IBindableAccessToProtectedFunctionality. Omit the "Tunnelled" prefixes.
    2. If you use BindableExt, create delegating methods within MyViewModel for every method the BindableExt instance exposes through its explicitly implemented interface IBindableExtAccessToProtectedFunctionality. Omit the "Tunnelled" prefixes.
  4. Intercept every method call to ViewModelBase which ultimately results in raising the PropertyChange event and forward it instead to the Bindable instance's TunnelledNotifyPropertyChanged method.
  5. Forward every PropertyChanged event that the Bindable instance raises to ViewModelBase's PropertyChanged event.
  6. Implement the IDependencyFrameworkNotifyPropertyChangedInTransaction interface in MyViewModel
    1. Forward the interface's FirePropertyChanged method to the Bindable Instance's, which also implements the IDependencyFrameworkNotifyPropertyChangedInTransaction interface.
    2. Forward every PropertyChangedInTransaction event that is raised by the Bindable Instance to the PropertyChangedInTransaction event MyViewModel now exposes.


I've implemented such an intermediate ViewModel class for MVVMLight. You can find the full example here. In this scenario I've chosen to use PDFx's BindableExt version (See Library Versions for more information).

How to use PDFx with a framework different from MVVM Light

In case you don't use MVVM Light, you can still use the intermediate class i've implemented for MVVM Light as a starting point and follow a few simple steps to make it work with your framework:
  1. Get the intermediate class's source code from here.
  2. Change the intermediate class's base class from MVVMLight's ViewModelBase to the ViewModel base class that comes with your framework.
  3. Change the intermediate class's MVVM Light Specific region
    1. Override whatever method your base class uses to ultimately raise the PropertyChanged event and forward it to the PDFx's TunnelledNotifyPropertyChanged method.
    2. Change the FirePropertyChanged method's implementation to call your base class's method which raises the PropertyChanged event. This should be the very same method you overwrote in the previous step.

If you want to make sure that you followed the steps correctly and that the PDFx is ready to use, I recommend running the MVVMLightExtension's Unit Tests against your new intermediate ViewModel.




Last edited May 16, 2013 at 7:56 PM by KevinStumpf, version 7

Comments

No comments yet.