How to Create a Custom Xamarin Forms Template

DFoulkDFoulk USMember ✭✭✭

We are trying to create a Xamarin Forms "boilerplate" for our company's enterprise Xamarin Forms applications. Each application that we have made requires the integration of our company's "common libraries" (security, theme, etc). We simply need to be able to create a new Xamarin Forms app that includes all of these libraries, NuGet packages and common pages/viewmodels/models.

One idea is to create a Visual Studio Template. Not sure if this is the "correct" way to do this because you only have the option of exporting one project in your solution (to a template). We would like to export our App, App.Android, App.iOS & App.UWP projects as a template. Even if we were able to export these projects (combined) as a template, would the GUIDs generated by Xamarin be "re-generated"? Would the App names be populated? Not sure...

Anyways, we have created a "boilerplate" Xamarin Forms app that we would like to export as a Visual Studio template. How do we create Xamarin Forms Visual Studio templates (how do you turn a Xamarin Forms solution into a custom template in the "Cross Platform App (Xamarin)" => "Blank/Master Detail/Our Custom Template" menu)? If there is a different way to accomplish similar ends, then we are happy to explore it!

Best Answer

Answers

  • DFoulkDFoulk USMember ✭✭✭

    I just found this documentation on multi-project templates:

    https://docs.microsoft.com/en-us/visualstudio/ide/how-to-create-multi-project-templates

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    What if you make a complete solution with all the common projects as references to existing common projects. As references they remain up to date since there is only one 'real' version.

    Name that solution "Dummy"

    When you need to make a new app duplicate the "Dummy" solution - then do a global find/replace - Replacing "Dummy" with "OurCoolAppFive" or whatever the new app is supposed to be.

    Then add the new app to your source control for the rest of the team.

  • DFoulkDFoulk USMember ✭✭✭

    @ClintStLaurent said:
    What if you make a complete solution with all the common projects as references to existing common projects. As references they remain up to date since there is only one 'real' version.

    Name that solution "Dummy"

    When you need to make a new app duplicate the "Dummy" solution - then do a global find/replace - Replacing "Dummy" with "OurCoolAppFive" or whatever the new app is supposed to be.

    Then add the new app to your source control for the rest of the team.

    A totally viable option! Then just manually go through and update the GUIDs with a new value? That might work...

  • DFoulkDFoulk USMember ✭✭✭

    I've found the Visual Studio documentation for creating multi-project templates with template parameters.

    I think I have the tools to produce the template that I initially wanted to create, but it looks like (somewhat) of a daunting task. Gonna have to weight the time-to-benefit ratio for our initial idea and @ClintStLaurent's idea...

  • HunumanHunuman GBMember ✭✭✭✭

    Hi @DFoulk

    It does sound daunting but its not difficult once you get started.

    The first time I did it last year I used this walk through to guide me, not much has changed since then AFAIK,

    Hope this helps,

    Tim

  • DFoulkDFoulk USMember ✭✭✭

    @Hunuman said:
    Hi @DFoulk

    It does sound daunting but its not difficult once you get started.

    The first time I did it last year I used this walk through to guide me, not much has changed since then AFAIK,

    Hope this helps,

    Tim

    Hey Tim! Thanks for the reply. I will give it a shot... Do you use template parameters in your template(s)? I'm having a hard time figuring out how to use them.

    For example:

    TestProject > Test.cs

    namespace $safeprojectname$
    {
        public class $safeprojectname$
        {
        }
    }
    

    Using the above, not only do the names not render in the new project- this entire file is not created... I'm reading that you modify the .vstemplate file to include files, but that file is not created until after you run the 'Export Template' wizard? Do you by chance have any supplemental information on this?

    Again, I appreciate any assistance you may provide! Thanks

  • DFoulkDFoulk USMember ✭✭✭
    edited August 2017

    Thank you all for your assistance. I ended up figuring this out (at least on a small scale with a solution containing x1 console app & x1 class library with some dummy classes).

    For anyone who wants to create their own Xamarin Forms Visual Studio template:

    1. Research the "Export Template" option in Visual Studio (how it works & how to create/modify a .vstemplate file)
    2. Research single-project templates
    3. Research template parameters
    4. Practice creating a simple template with parameters
    5. Research multi-project VS templates
    6. Practice creating a multi-project template
    7. Apply what you've learned by creating a Xamarin Forms template (which is simply a multi-project template that uses template parameters)

    I will try to add a sample here to help people in my situation once I have completed my template! If I forget, then just post on this thread (requesting the sample) and I will do my best to supply it for you...

    Thanks

  • DFoulkDFoulk USMember ✭✭✭

    @PierceBoggan said:
    If you're creating templates and your team is working on Visual Studio 2017, I would highly suggest you look into the new templating engine.

    I have some really basic notes here on how to do it for a Xamarin project:
    https://github.com/pierceboggan/dotnet-templating

    Advantages:

    • Your template "apps" still build. You don't have to use template parameters like $safeprojectname which prevents your template from being easily compilable.
    • Share the same templates across multiple OSes.
    • You have just as much, if not more, flexibility using this templating engine than the "built in" Visual Studio templating engine.

    Thanks @PierceBoggan! I will check this out tomorrow. I appreciate the resource(s)!

  • HunumanHunuman GBMember ✭✭✭✭
    edited August 2017

    Thank you @PierceBoggan

    The new templating engine looks very cool, I will have to give it try as soon as I get some down time.

    @DFoulk
    Glad you have it sorted, and the sample would be really useful IMO.

    Thanks again,

    Tim

  • DFoulkDFoulk USMember ✭✭✭

    @Hunuman said:
    Thank you @PierceBoggan

    The new templating engine looks very cool, I will have to give it try as soon as I get some down time.

    @DFoulk
    Glad you have it sorted, and the sample would be really useful IMO.

    Thanks again,

    Tim

    Good deal, I'll post it and share a link here shortly :)

  • DFoulkDFoulk USMember ✭✭✭

    Haven't forgotten to post the sample, just haven't had a chance to put it together yet! I was going to post our template, but (unfortunately) there are some security issues with me doing so, so I will have to make a new blank project and share that. Bear with me!

  • DFoulkDFoulk USMember ✭✭✭
    edited August 2017

    Good god- you guys would not believe what it has taken to make this work... I am going to have to put together a full tutorial with screenshots and downloads of some helper PowerShell scripts.

    Anyways, I ran into a bunch of problems with template parameters. The end result is what I had hoped for, save for a few missed parameter replacements. BUT- nevertheless, I figured it out!

    I sure hope the new templating engine that @PierceBoggan referenced is easier to work with. Multi-project templates are a nightmare in the current engine...

    Gonna start putting the tutorial together this weekend (gotta do that on my own time). Stay tuned!

  • learningMobilelearningMobile USMember ✭✭

    @DFoulk I am in the process of creating a multiproject template and wondering if you are willing to share the code.Did you ever put together a tutorial as you said in your post?
    thanks

  • DFoulkDFoulk USMember ✭✭✭

    Microsoft finally released some documentation:

    How to Create Multi-Project Templates

    I did not use that documentation however... I wanted to create a multi-project template with "global" variables. If this is you, then use this tutorial (adopting the solution for Xamarin.Forms):

    Build a Multi-Project Visual Studio Template

  • DFoulkDFoulk USMember ✭✭✭

    The second link in the above will do a much better job illustrating the necessary steps than I could. Please let me know if you have any questions after working through that tutorial. Unfortunately, I cannot share our company code- and producing a sample was taking too long (I do all of this while at work- so gotta keep it short).

    Finally, I will tell you that the Visual Studio templating engine is not user-friendly. There are many things missing and many limitations (as you can see from the tutorial by Tony Sneed)... SO- if you find a tutorial that uses a 3rd party extension; it may be worth giving it a shot!

    Good luck,
    -Derek

  • SagarPanwalaSagarPanwala USMember ✭✭✭
    edited August 29

    @DFoulk : Unfortunately second link is not about Xamarin Forms. I'm also wanting to make some template. I'm using 1 core project in all of my projects. So when I create new projects, I want to add that core project from specific path in local computer. I'm using .net standard and with VS for Mac.

    I have found two samples , but I'm not sure what to do with them and are they relevant or not.
    https://github.com/AlessandroDelSole/XamarinFormsSnippets
    https://github.com/picolyl/XamarinFormsTemplates

    Can you please guide me how can we do this ?

  • DFoulkDFoulk USMember ✭✭✭

    @SagarPanwala Though the second link isn't explicitly about Xamarin.Forms, it is about creating a template for a multi-project template in Visual Studio. I am not working today, but when I get back on Monday- I will do my best to allot some time to revisit how I did this. If you want a headstart and wish to do this using the baked-in templating engine in VS, please follow the guides I linked in my comment (especially the second one). I did so (adapting the steps to Xamarin.Forms) and have a working template.

    I do apologize for not having written a large, beautiful step-by-step guide on how to do this. I am a member of a small mobile dev team that has a ton to do each week :(

Sign In or Register to comment.