Some Hints at Creating Reusable User Controls in Xamarin.Forms in Visual Studio 2017

Lately I have been using Xamarin Forms, part of Microsoft Visual Studio 2017 Enterprise, and wanted to share a trick with creating user controls. In Xamarin there is no such thing as a “User Control” similar to the days of Classic Windows Forms applications. Through the progression of the Universal Windows Platform (UWP) and technologies used by Xamarin we now have the concepts of Controllers, Models, Pages, and Views.

Now. What do you do if you want to create a reusable component or user interface control (UI/UX) in Xamarin Forms?

Don’t Panic. First layout what your control should do, which properties it should expose through code and the XAML code space for the Xamarin.Forms previewer.

Second, do not assume there will be a simple User Control template. Instead — wait for the big hint — use the “Content View” template (no… do not use anything “Content Page”).

Layout your component in the file, but prepare for the easy next step.

Inside the Pages that use the specific component you first will want to import the namespace with the Content View using the proper “xmlns:” namespace. Visual Studio 2017 will do a nice job in helping you fill the proper information with Intelli Sense.

Now inside the page it will be very easy to add the control. It is very simple with XML logic — “<namespace:MyContentView />”.

Open the Xamarin Forms previewer and now you will see a nice new component that you designed. The plus side is now you can re-use as many times as your heart desires within the same project or even from a Shared .NET Standard 2.x project.

Enjoy these wonderful hints to get you started with reusable UI/UX development in Xamarin Forms!

Leave a Reply

Your email address will not be published. Required fields are marked *