Using Prism modularization in Xamarin.Forms

Recently, Prism for Xamarin.Forms 6.2.0 has been released with many notable improvements including a new bootstrapping process, AutoWireViewModel behaviour changes, deep-linking support,  modularity and Prism template pack enhancements (full release notes available here).

Today, I fired up Visual Studio to have a play with this new version and decided to try the Xamarin.Forms support for Prism Modules: this is a very useful feature which allows to separate clearly the various parts of the application and improve quality, extensibility and readability of the code.

After downloading the new template pack, I created a new solution selecting New Project => Templates => Visual C# => Prism => Forms => Prism Unity App:

PrismTemplatePack

The new wizard is very useful and permits to select the platforms to target in the project: I selected Android, iOS and UWP and the project was generated targeting the three platforms with a shared PCL. NuGet packages were already updated to the latest version so no need for further actions.

While exploring the new project structure and the new modularization stuff, I decided to create a new Xamarin.Forms portable class library containing a module with a single View/ViewModel (SamplePage SamplePageViewModel) visualised when a user interacts with a button on the home page.

The new module required the definition of the following class implementing the Prism IModule interface:

To keep the logic separated from the rest of the app, I decided to register the navigation type for SamplePage inside the Initialize() method of the module which will be triggered when the module loads.

I also applied Xamarin.Forms XAML compilation to the module to improve performance, which is always great to have 😉

It’s worth noting that in this new Prism release the default value for the attached property ViewModelLocator.AutowireViewModel is set to true, so we can omit it and the framework will automatically associate SampleViewModel as the BindingContext for the view:

I then explored the new breaking changes for the bootstrapping process: the application class now needs to inherit from the PrismApplication class and two new virtual methods OnInitialized() and RegisterTypes() permit respectively to specify the implementation for navigating to the home page and registering the known types / ViewModel’s for navigation:

The third overridden method, ConfigureModuleCatalog(), informs the app to initialize the catalog with the module we created previously and set the initialization mode to OnDemand which means the module is not activated when the application starts, but it must load explicitly. This feature is particularly useful in cases in which some functionalities of the app must initialise after some other requirements like, for instance, successful authentication or applications extended via modules.

The sample view was in place, so I proceeded with the implementation of the HomePage: I wrapped the existing one in a NavigationPage to allow the correct back stack and then created two commands for initializing the module and navigating to the SamplePage defined previously:

and the corresponding ViewModel:

The module is initialized by injecting the Prism ModuleManager and then calling the LoadModule() method:

The navigation to the page defined in the module is performed by:

The property IsSampleModuleRegistered permitted to control the CanExecute() for the button commands using this nice fluent syntax using ObservesProperty(()=>….) available in Prism:

Here we go: I found the Prism implementation in this new version very stable and working well with Xamarin.Forms. The modularization capabilities are useful to write clean, maintainable and extensible apps.

The source code is available for download as part of the official Prism samples on GitHub.

Looking forward to exploring all the other capabilities available in Prism and Xamarin.Forms. Happy coding!

Xamarin.Forms: sharing resources in XAML using Styles

Previously, I’ve blogged about creating a custom Behavior in Xamarin.Forms. The sample published on GitHub initially used the default template available in Visual Studio which instantiates the Application singleton from C# using the following App class:

This is a great approach, however very often it is useful to define common resources in XAML and reuse them across the application using Styles.

To achieve this, It’s possible to transform the App class in a partial one and add a new App.xaml source file containing a resource dictionary to be shared:

In this code, I’ve defined a new resource with a specific Padding to be applied only on iOS via the OnPlatform class, and an explicit style named PageStyle targeting ContentPage types.

The modified code behind is now taking care of parsing/loading the XAML (via the InitializeComponent() call) and then creates an instance of SamplePage:

The page can now consume the new style specifying Style=”{StaticResource PageStyle}” (dynamic resources are also supported):

This is a basic example of using styles in Xamarin.Forms: a complete coverage of the various options is available on the Xamarin official documentation.

The source code is available for download here.

Having fun with Xamarin.Forms and Multi-Touch Behaviors

Recently Xamarin has released preview support for the Universal Windows Platform in their Xamarin.Forms framework so I have been playing around with version 2.0 for testing its features and verify how easy is to target multiple platforms (iOS, Android, Windows 10 UWP, Windows Phone, Windows 8.1) with a single codebase.

One of the experiments I have done is related to custom multi-touch gestures: the idea to use a XAML Behavior is a common scenario to write well structured code so I started creating a new Cross-Platform Xamarin.Forms Portable project and upgraded the NuGet packages to the latest stable version of the framework (currently v2.0.1.6505).

I have then read the official documentation and analysed the samples available on GitHub: a very good example is the PinchGesture one so, starting from it, I created a new MultiTouchBehavior implementing this gesture and attached the same Behavior to an Image object added to a sample ContentPage, as described below in this lovely cross-platform XAML 🙂

The BindingContext=”{Binding}” is used to trigger the BindingContextChanged event and initialise correctly the GestureRecognizers  for the parent object since the AssociatedObject.Parent is set to null when the Behavior.OnAttachedTo() is called (I suppose that the XAML tree is not yet completely created when the behavior is attached in Xamarin.Forms):

Here is the project deployed to the Android, iOS and Windows 10 emulators:

MultiTouch_Android_Emulator   Simulator Screen Shot 14 Feb 2016, 18.25.00   MultiTouch_Windows10_Emulator

I’ve been particularly impressed by Xamarin.Forms: the possibility to target so many platforms using the same code is a killer feature and the development environment is also very comfortable to use.

The sample code used in this post is available on GitHub, I’m planning to add more functionalities in the future with particular regard to other common multi-touch gestures using Xamarin.Forms and XAML.

Cordova Windows Store apps and certificate expired error

I was updating TypedMVVM to the latest Visual Studio 2013 Update 4 and Cordova tools (using the new free Community Edition) and trying to deploy the sample to the Windows 8 emulator when I received the following

error APPX0108: The certificate specified has expired

As pointed here by Microsoft Open Technologies, the certificate available in the Visual Studio template has just expired and is not possible to deploy Windows Store apps using the old one.

To solve this problem I’ve then downloaded from here the updated certificate and copied to the following location inside the Visual Studio project:

res/native/windows/CordovaApp_TemporaryKey.pfx

The updated code is available for download on CodePlex.

TypedMVVM: Samples updated for Apache Cordova

I’ve recently installed the Multi-Device Hybrid Apps template on Visual Studio in order to try out the development of native mobile applications (targeting Android, iOS, Windows Store and Windows Phone) using HTML, CSS and JavaScript using the Apache Cordova framework.

As a first example, I decided to reuse all the TypeScript code available in my library TypedMVVM available on CodePlex.

The process was very simple and didn’t take long: I was able to quickly create a basic “Hello World” application successfully deployed on the Ripple – Nexus 7 Android emulator

TypedMVVM_Cordova

As usual, the updated code is available for download on Codeplex.

Universal Windows Apps, WinJS and TypedMVVM: samples updated to target Windows Phone and Windows Store apps

I’ve just returned from the Build 2014 conference in San Francisco where I had the opportunity to attend several interesting sessions about new developments in the Microsoft space.

One of the “big news” has been the introduction of the concept of “Universal Windows Apps“, a great way to share code between different platforms including (but not limited to) Windows Phone, Windows Store and Xbox One apps.

I decided to test this new feature on TypedMVVM, one of my “pet projects” which I made available on CodePlex some time ago. The project was originally using a simple MVVM infrastructure targeting Windows Store apps using WinJS and TypeScript so it was an ideal candidate for adding a Windows Phone 8.1 target reusing all the existing code.

The migration to Universal Apps was really straightforward: I was able to have the samples up and running in my emulator and devices while waiting at the airport in San Francisco.

TypedMVVMWindowsPhoneSample

TypedMVVMWindows81Sample

I’ve uploaded all the source code on Codeplex here, feel free to grab and give it a try.

Happy universal coding everyone! 🙂

MultiTouch Behaviors available on NuGet

I’ve received several requests for publishing the MultiTouch Behaviors on the NuGet gallery so I created a simple package containing support for the Windows Phone and Silverlight platforms.

To enable your project for MultiTouch, just run the following command in the Package Manager Console and all the libraries and dependencies will be added to the Visual Studio project:

PM> Install-Package MultiTouchBehaviors

Alternatively just search for the package selecting Tools->Library Package Manager->Manage NuGet Packages for Solution from Visual Studio 2012.

If you are interested in creating a NuGet package for your open-source project, register an account here in the gallery and follow the instructions described in the section “Creating and Publishing a Package“: they are very clear and contain detailed information about the process.

The package definition for MultiTouch is also available in the Source Code section on CodePlex.

Happy Coding!

Windows Phone 8 Multi-Touch Behaviors available on CodePlex

The last week has been very exciting for Microsoft developers: The //build/ conference in Redmond was full of amazing content including Windows 8Windows Phone 8 SDK, Microsoft Surface, Xbox SmartGlass, Kinect for Windows and the new language TypeScript.

Unfortunately I hadn’t the possibility to participate at the conference but had some free time during the week-end to watch some of the recorded sessions available here on Channel9: this is really a great resource and I suggest everyone interested in Microsoft technologies to download some videos and have fun with it.

I decided then to take a look at the new features available in Windows Phone 8 and the related SDK: Justin Angel has a great article about this topic in the Nokia Developer Community.

To get my hands dirty I moved then on my personal Open-Source project “Multi-Touch Behaviors” on CodePlex: this is a set of Epression Blend Behaviors for different platforms including Windows Phone, Silverlight and WPF for applying gestures and manipulations (like the one available in the “old” ScatterView control) to generic XAML elements.

The project was not yet updated to Windows Phone 8 so I took this opportunity and experimented with the new SDK: this was really a great experience since I was able to upgrade quickly all my samples to the new platform using the same codebase used for the Silverlight version.

Unfortunately I haven’t the possibility  to test the performance on a real Windows Phone 8 device at the moment but the first impressions are really positive.

I have already checked in the code for WP8 here, I’m planning more testing and development in the future.

Happy XAMLing everyone 🙂

Multi-Touch Behaviors updated: Windows Phone “Mango” support, Manipulation / Inertia Processors and more

I’ve just published on CodePlex and the Expression gallery a new release of the Multi-Touch behaviors including these new features:

  • support for Windows Phone 7.1 Beta 2 (refresh) “Mango”;
  • added new property “IgnoredTypes” for excluding particular control types from the manipulations (thanks to Richie for the suggestions, feedback and code samples);
  • the “Manipulation Processor” and “Inertia Processor” are now exposed by the behavior in order to enable personalized manipulations and gestures;
  • new properties: CenterX, CenterY, Rotation, Scale permit to support custom gestures like “DoupleTap” zoom;
  • Silverlight 4 and Windows Phone 7.1 samples updated with a simple “DoubleTap” zoom example using the new exposed properties.

The source code and samples are available for download here.

Happy Silverlighting!

Using the Multi-Touch Behavior in a Windows Phone 7 Multi-Page application

In these days I’m receiving several questions in the Multi-Touch Behaviors forums about enabling Multi-Touch manipulations in Windows Phone applications composed by more than one page.

Today I’ve modified the available sample inserting an additional page containing an image enabled for translation, rotation, zoom and inertia via the usual “MultiTouchBehavior“:


Starting from release 0.6.1 it’s now possible to use the Behavior in different pages of the application, in this way enabling multi-touch in different elements of a Windows Phone 7 application.

As usually the sample code is available for download on http://multitouch.codeplex.com, check out the Visual Studio solution “SilverlightWP7MultiTouch.sln” which contains all necessary libraries.

Happy Silverlighting!