Dedicated to fans
of the Microsoft Patterns & Practices group and just to fans of such a useful thing as
Microsoft PRISM .
Developers who, in their practice, met with Microsoft PRISM, probably have a mixed opinion about this framework. On the one hand, in its number 4, and in order of the 3rd, the version is a very powerful and flexible tool for creating composite applications, and on the other hand, a rather incomprehensible and confusing library. But you can take a look at PRISM and simply as an implementation of the MVVM pattern, which is what PRISM for Windows Phone is.
I really like this MVVM implementation and when I met the Portable Class Library, I realized that I needed a portable PRISM. Fortunately, PRISM source code is available and it was not a very difficult task to convert it to a Portable Library.
Porting
The task itself was to bring everything in common to the portable part and everything else to the platform-specific. But, besides the fact that I like PRISM, I still like to use it with MEF, and
MEF just with the advent of PCL2.0 appeared for WinRT in the form of
Microsoft.Composition .
')
By the way, who does not know, Microsoft.Composition is built on PCL and, if desired, it can be built for PCL and used on Windows Phone 8 or directly in a portable library. It is true that I didn’t find the latest version of the source code for which the NuGet Microsoft.Composition package was built, on the project site on Codeplex .
So the MEF extensions for PRISM for WinRT had to be thoroughly modified so that PRISM could work with Microsoft.Composition. However, the majority of unit tests were left behind by making small changes.
Abstraction and Fody
Who fundamentally understood how PRISM works knows that there are composite events that require Dispatcher, but it’s different on different platforms. In addition, the version for .NET used the
WeakReference type, which is not available on some platforms. And I would not like to force someone who will use Portable PRISM every time to write some kind of initialization code that would implement these dependencies.
And then I learned that each .NET assembly has an initializer - in fact a static constructor that runs once when the assembly is loaded into the application, but you can’t just write it ... And then the great tool of the Australian Simon Cropp came to my rescue,
Fody .
This project was born from the extension project for Visual Studio Notify Property Weaver , which allowed, using Mono.Cecil after construction, to rewrite the properties of classes that were inherited from INotifyPropertyChanged so that they themselves would trigger a property change event when its value changes.
Fody has become a more universal extension of
Notify Property Weaver , to which you can connect different plug-ins that will overwrite a specific part of the assembly. For
Fody , many
useful modules have already been
written that can be put through NuGet into a specific project. One of these plugins is
ModuleInit.Fody , which can write a call to the Initialize method of the static class ModuleInitializer in the assembly initializer.
internal static class ModuleInitializer { public static void Initialize() { InitializeCompositePresentationEvent(); InitializeWeakEventHandlerManager(); } private static void InitializeCompositePresentationEvent() { var dispatcher = new Lazy<IDispatcherFacade>(() => new DefaultDispatcher()); EventBase.InitializeDispatcher(dispatcher); } private static void InitializeWeakEventHandlerManager() { EventHandlerManager.Current = new WeakEventHandlerManager(); } }
Than I safely and took advantage.
Funny, but ModuleInitalizers don't work on Windows Phone 7 . And the impression that no one is going to fix this bug. Well now, at least static constructors work in plug-in assemblies .
Port Features
Winrt
The version for WinRT does not include
regions . I didn’t need them, but writing an adapter for Frame is not possible, so I decided not to transfer this functionality. Nevertheless, the source code in the project is. So, suddenly someone needs to add them, you can do it.
Windows Phone 8
In PRISM for Windows Phone 8, I generally copied the PRISM functionality for Windows Phone 7 by adding only WeakEvent, since it is now available in the Windows Phone SDK 8.0 and removed the classes related to AppBar since they are implemented there extremely crookedly. I think it's much better to use the
Cimbalino library to work with
AppBar .
What is available now?
And so what are the NuGet packages there are:
What's next?
I hope that my packages will be useful to those who like PRISM or who want to implement something using PRISM to a new platform.
If someone needs other parts of PRISM in the form of a portable version, for example, extensions for Unity for WinRT, you can clone
the Portable PRISM project on GitHub and add them. If you can not build a project, write. I have not figured out how to configure .gitignore so that information about NuGet packages is committed, but the packages themselves are not, and they can be downloaded again. So, who knows how to do it, tell me, please.