Even admitting I accept it may not be the appropriate technology for every project, I’m a huge fan of Xamarin Forms. As you’ll apparently now if you chase my blog, I’m a Windows Developer and Xamarin Forms allows me to use basically all my accepted abilities (XAML, binding, MVVM, etc.) to actualize applications additionally for added accepted belvedere brand iOS and Android. Additionally, it gives me the adventitious (like with built-in Xamarin) to booty advantage of belvedere specific appearance and, at the aforementioned time, to advance a user acquaintance which is articular with the attending and feel of the operating system.
I’ve afresh acclimated Xamarin Forms to actualize a porting for Android of my simple Qwertee Shirts app and the advantage was clear: I was able to reclaim best of the backend cipher I’d already accounting for the UWP adaptation and my XAML ability but, at the end, I got an appliance that absolutely embraced, from a UI point of view, the new Material Design created by Google so it doesn’t attending “alien” like it generally happens with cross-platform applications based on web technologies.
However, I’m not alone a Windows Developer but additionally an MVVM enthusiast and I’ve blogged about this affair assorted times, accoutrement assorted platforms and frameworks. If you’re new to the MVVM pattern, I advance you alpha from this column and move on with the blow of the series. The aboriginal affair I did back I absitively to resume alive with Xamarin Forms was to anchorage my app. I was attractive for the best way to reclaim my MVVM ability to advance the activity and, as usual, the best was hard. In this case, it was alike added complicated because Xamarin Forms, compared to added XAML technologies like WPF or UWP, is appealing new, so it was adamantine to acquisition a best that absolutely annoyed me.
Don’t get me amiss — if you bethink my posts about Template10 for UWP apps, you’ll apperceive that I’m a huge fan of the adaptability offered by MVVM Light and Laurent Bugnion did a superb job to acquaint archetypal MVVM concepts (like bounden and commands) in platforms that don’t natively abutment them, like Android and iOS. However, Xamarin Forms is a bit altered than accepted Xamarin: it already offers the concepts we charge to advantage the MVVM pattern, like binding, abstracts context, annex properties, behaviors, etc. In this scenario, MVVM Light is still a abundant best but you still accept to reinvent the caster to break abounding accepted scenarios you accept to accord with back you’re developing an XAML app, like administration navigation, accepting admission to aeronautics contest in a ViewModel, or casual ambit amid one folio to the other.
Right afore starting my porting, I saw a cheep by Brian Lagunas, one of the MVPs abaft the Prism project, announcement a new adaptation of Prism specific for Xamarin Forms. Aloof to brace your mind, Prism is an MVVM framework that originally was created by the Patterns & Practices analysis of Microsoft and angry into an accessible antecedent activity apprenticed by the community. Prism has consistently been a abundant best to apparatus the MVVM arrangement in XAML based applications, but sometimes you may accept faced the accident to accomplish the activity over complicated aloof to chase its allotment conventions and rules (like the claim of accepting a bootstrapper to initialize it, admitting the actuality that XAML based appliance already accept a startup chic alleged App).
After commutual the porting, I’ve begin myself actual annoyed with the Prism admission for Xamarin Forms, so I’ve absitively to allotment my acquaintance with you, acquisitive that it will get you up and active quicker back you alpha alive on a new Xamarin Forms project.
The easiest way to actualize a Xamarin Forms activity based on Prism is to use its own Visual Studio addendum that you can download from the Visual Studio Gallery. After you’ve installed it, you will acquisition in Visual Studio a new area alleged Prism, with assorted templates for anniversary accurate technology. The arrangement we’re absorbed in is alleged Prism Unity App (Forms):
Actually, this arrangement has an advantage over the accepted Xamarin Forms template. As you can see from the angel below, it allows you to accept which belvedere you appetite to ambition back you actualize your project, while the absence Xamarin Forms arrangement automatically creates a activity for anniversary accurate platforms (Android, iOS, Windows Phone 8.1, Windows 8.1, UWP), alike if you aren’t absorbed in targeting one of them.
After you’ve hit the Actualize project, you will end up with a accepted Xamarin Forms solution: one Portable Chic Library and one specific activity for anniversary belvedere you’ve chosen. Additionally, the Portable Chic Library will already contain:
Here is what your absence activity will attending like:
To audience Prism for Xamarin Forms, I’m activity to actualize a simple applicant for TrackSeries, a TV Show website created by my abundant accompany and colleagues Adrian Fernandez Garcia and Carlos Jimenez Aliaga.
Let’s alpha from the alpha and see which references accept been automatically added by the arrangement to the project:
As you can see, added than the accepted Xamarin Forms NuGet package, Prism requires two packages: a Core one (which is in accepted beyond every platform) and a Forms one (which, instead, contains the specific cadre and casework for Xamarin Forms). By default, the accepted arrangement leverages Unity as its annex bang container, so you’ll acquisition a agglomeration of added bales like Unity, Prism.Unity.Forms and CommonServiceLocator. However, if you don’t like Unity, Prism for Xamarin Forms offers some added bales to accommodate added accepted annex bang containers, like Ninject or Autofac.
One of the better changes compared to the old Prism versions is the abatement of the bootstrapper concept, which was a committed chic of the activity that took affliction of initializing all the Prism infrastructure. Xamarin Forms (as every added XAML technology) already has an initializer class: the App one, which is included in the Portable Chic Library, so the aggregation has absitively to advantage it instead of allurement to the developer to actualize a new one. By default, this chic inherits from the Appliance class. To appropriately abutment Prism, we charge to change this and let the App chic accede from the PrismApplication one by:
Additionally, the App chic has three characteristic features:
The IPlatformInitializer constant is absent by default, but it can be leveraged in case you charge to annals in the annex alembic some specific classes that exist only in a belvedere specific project. You will find, in fact, that every belvedere specific activity has its own custom initializer chic (AndroidInitializer for Android, UwpInitializer for UWP, etc.) which, however, by absence has an abandoned accomplishing of the RegisterTypes() method. Here is what the MainPage.xaml.cs of the UWP activity looks like:
As you should already apperceive if you accept some antecedent acquaintance with MVVM , the key to authoritative the arrangement assignment is to affix the ViewModel with its own Appearance through binding. The alone aberration in a Xamarin Forms app compared to a Windows app is that the acreage to ascertain the ambience is alleged BindingContext and not DataContext. Prism makes use of a simple allotment assemblage to automatically accredit a ViewModel to its View:
As you can see, this is the exact basement that the Prism arrangement has created for us. Every folio that we add to our appliance needs to be registered in the container so that we can appropriately handle the navigation. To annals it, we can advantage the RegisterTypes() adjustment of the App chic and use one of the methods offered by the Alembic alleged RegisterTypeForNavigation<T>, area T is the blazon of the page. In the starting template, we accept aloof one folio alleged MainPage, so it’s the alone one that is automatically registered back the appliance starts. Here is one of the better differences amid Prism and added MVVM frameworks. With added toolkits, you are acclimated to annals in the alembic alone the ViewModels and, eventually, all the casework accompanying to them. With Prism, instead, you aloof annals the page’s type: it’s up to Prism to automatically annals in the alembic additionally the ViewModel affiliated to the View. As you can see in the sample code, we accept registered the MainPage chic and not the MainPageViewModel one.
If you aren’t a fan of the allotment assemblage approach, you aren’t affected to use it: in fact, the RegisterTypeForNavigation() adjustment has addition variant, which signature is RegisterTypeForNavigation<T, Y>(), area T is the page’s blazon and Y is the ViewModel’s blazon we appetite to set as BindingContext. So, for example, if you appetite to affix your MainPage to a ViewModel alleged MyCustomViewModel, it’s abundant to annals it appliance the afterward code:
In the OnInitialized() adjustment you can see a examination of how aeronautics works by default: every time you alarm the RegisterTypeForNavigation<T> method, Prism registers into the NavigationService a advertence to that folio using, as key, a cord with the aforementioned blazon name. Since our page’s blazon is MainPage, we charge to canyon the cord “MainPage” as constant of the NavigateAsync() adjustment to activate the aeronautics to that page. If we appetite to override this behavior, we can canyon as constant of the RegisterTypeForNavigation<T>() a custom cord and use it for the consecutive navigations, like in the afterward sample, area we accept replaced the key “MainPage” with the “MyCustomPage” one.
However, in the aing posts, we’ll see added capacity about how to handle aeronautics in a added avant-garde way.
One of the appearance I’ve accepted best in Prism for Xamarin Forms is that it doesn’t crave us to accomplish any changes in XAML folio to abutment it (for example, some added MVVM frameworks crave you to change the ContentPage blazon with a custom one).You will alone find, in the MainPage.xaml file, a specific Prism attribute, as acreage of the ContentPage entry, alleged ViewModelLocator.AutowireViewModel:
This acreage takes affliction of aing the Appearance with the ViewModel: back it’s set to true, the ViewModel will be automatically set as BindingContext of the Appearance if we accept admired the allotment assemblage ahead described. However, one of the changes alien in Prism 6.2 is that this acreage isn’t appropriate anymore unless you appetite to absolutely attenuate the allotment assemblage by ambience it to false. The accepted arrangement adds it to accord a added complete sample, but you can cautiously aish it.
A key affection offered by every MVVM framework is a chic to use for our ViewModels to accord quick admission to the best acclimated features, like the accomplishing of the INotifyPropertyChanged interface. Prism doesn’t accomplish any barring and it offers a chic alleged BindableBase, which our ViewModels can accede from:
Thanks to this class, whenever we charge to actualize a acreage that accouterments the INotifyPropertyChanged interface (so that it can bear its changes through the bounden channel) we can artlessly use the SetProperty() adjustment in the setter of the property. This adjustment will booty affliction of autumn the amount and, at the aforementioned time, sending a notification to all the controls that are in bounden with this acreage that its amount has changed, so they charge to amend their layout.
The sample app created by the arrangement does absolutely this: it creates a acreage alleged Title, which is affiliated through bounden to a Label ascendancy in the XAML page. Whenever we change the amount of the property, we will see the UI adapted in absolute time. To be honest, the sample app shows article abroad too: it sets the amount of the Title acreage in a adjustment alleged OnNavigatedTo() and it parses some parameters. We’re activity to see how this admission works added in capacity in the aing post.
In this post, we accept aloof aching the apparent and we showed the basal concepts abaft a Xamarin Forms appliance created with Prism. In the aing post, we’ll see some added avant-garde concepts, like administration aeronautics in a ViewModel or registering added casework in the annex container.
6 Secrets You Will Not Want To Know About Xamarin Forms Xaml Designer | Xamarin Forms Xaml Designer – xamarin forms xaml designer
| Encouraged to be able to my personal blog, on this occasion I’m going to demonstrate regarding xamarin forms xaml designer