In this project we'll take existing XAML pages and wire them together into an application using the common page types of navigation, tabbed, Carousel, and modal as well as add some basic styling to create a presentable application.
If you want to use Visual Studio (highly recommended) follow the following steps:
- If you already have Visual Studio installed make sure you have .NET Core installed by running the "Visual Studio Installer" and making sure ".NET Core cross-platform development" is checked.
- If you need to install Visual Studio download it at https://visualstudio.microsoft.com/ by selecting "Community 2019" from the "Dowload Visual Studio" drop down list. (If you're using Windows you'll want to check "ASP.NET" and ".NET Core cross-platform development" on the workloads screen during installation.)
- Open the .sln file in Visual Studio.
- To run the application simply press the Start Debug button (green arrow) or press F5.
- If you're using Visual Studio on Windows, to run tests open the Test menu, click Run, then click on Run all tests. (results will show up in the Test Explorer)
- If you're using Visual Studio on macOS, to run tests select the
XAMLInXamarinFormsTestsProject, then go to the Run menu, then click on Run Unit Tests. (results will show up in the Unit Tests panel)
(Note: All tests should fail at this point. This is by design. As you progress through the project, more and more tests will pass. All tests should pass upon completion of the project.)
If you would rather use something other than Visual Studio:
- Install the .NET Core SDK from https://dotnet.microsoft.com/download once that installation completes, you're ready to get started!
- To run the application go into the
XAMLInXamarinFormsproject folder and typedotnet run. - To run the tests go into the
XAMLInXamarinFormsTestsproject folder and typedotnet test.
- A Navigation Page
- A Tabbed Page
- A Carousel Page
- Basic Styling
- A Modal
Note: This isn't the only way to accomplish implementation. However, this is what the project's tests are expecting. Implementing the features in a different way will likely result in being marked as incomplete / incorrect.
- Create a new
TabbedPagein theViews/Nutritionfolder with the nameNutritionView.- When you create the
TabbedPagein Visual Studio (or similiar) the following code should be generated for you, if not you'll need to add it yourself. (Note: If you have to make this yourself make sure theNutritionView.xaml.csfile inherits theTabbedPageclass.)- The first line should be the
xmltag<?xml version="1.0" encoding="utf-8" ?>. - The second line should be an opening
TabbedPagetag with the following attributes:xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"xmlns:d="http://xamarin.com/schemas/2014/forms/design"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"x:Class="XAMLInXamarinForms.Views.Nutrition.NutritionView"
- The last line should be the closing
TabbedPagetag.
- The first line should be the
- In our
TabbedPage's opening tag add an attributexmlns:nutritionviewswith a value"clr-namespace:XAMLInXamarinForms.Views.Nutrition". - Between the
TabbedPagetags, add opening and closingTabbedPage.Titletags containing the valueNutrition. - After the closing
TabbedPage.Titletag, add a new self closing tag with the of typenutritionviews:BreakfastView. - After the
nutritionviews:BreakfastViewtag, add a new self closing tag with the typenutritionviews:LunchView. - After the
nutritionviews:LunchViewtag, add a new self closing tag with the typenutritionviews:DinnerView. - After the
nutritionviews:DinnerViewtag, add a new self closing tag with the typenutritionviews:SnackView.
- When you create the
- Create a new
CarouselPagein theViews/Fitnessfolder with the nameFitnessView.- When you create the
CarouselPagein Visual Studio (or similiar)the following code should be generated for you, if not you'll need to add it yourself. (Note: there is a good chance you won't findCarouselPagewhen creating the new page, if not useTabbedPageand change theTabbedPagetags toCarouselPageand make sure theFitnessView.xaml.csfile inherits theCarouselPageclass instead of theTabbedPageclass) - - The first line should be the
xmltag<?xml version="1.0" encoding="utf-8" ?>.- The second line should be an opening
CarouselPagetag with the following attributes:xmlns="http://xamarin.com/schemas/2014/forms"xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"xmlns:d="http://xamarin.com/schemas/2014/forms/design"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d"x:Class="XAMLInXamarinForms.Views.Fitness.FitnessView"
- The last line should be the closing
CarouselPagetag. - In our
CarouselPage's opening tag add an attributexmlns:fitnessviewswith a value"clr-namespace:XAMLInXamarinForms.Views.Fitness". - Between the
CarouselPagetags, add opening and closingCarouselPage.Titletags containing the valueFitness. - After the closing
CarouselPage.Titletag, add a new self closing tag with the of typefitnessviews:SundayView. - After the
fitnessviews:SundayViewtag, add a new self closing tag with the of typefitnessviews:MondayView. - After the
fitnessviews:MondayViewtag, add a new self closing tag with the of typefitnessviews:TuesdayView. - After the
fitnessviews:TuesdayViewtag, add a new self closing tag with the of typefitnessviews:WednesdayView. - After the
fitnessviews:WednesdayViewtag, add a new self closing tag with the of typefitnessviews:ThursdayView. - After the
fitnessviews:ThursdayViewtag, add a new self closing tag with the of typefitnessviews:FridayView. - After the
fitnessviews:FridayViewtag, add a new self closing tag with the of typefitnessviews:SaturdayView.
- The second line should be an opening
- When you create the
- Setup navigation to the
FitnessViewandNutritionViewon theViews/NavigationPageView.- Add click events to our
Views/NavigationPageView.xaml.csfile by adding the following methods.- Add
usingdirectives forXAMLInXamarinForms.Views.FitnessandXAMLInXamarinForms.Views.Nutrition. - Create a new
privateasyncmethod with a return type ofvoidnamedFitnessButton_Clickedthat accepts arguments of typeobjectnamedsenderandEventArgsnamede.- This method should contain only one line of code that calls
Navigation.PushAsyncmethod onthiswith an argument of newly instantiatedFitnessView. Don't forget toawait!
- This method should contain only one line of code that calls
- Create a new
privateasyncmethod with a return type ofvoidnamedNutritionButton_Clickedthat accepts arguments of typeobjectnamedsenderandEventArgsnamede.- This method should contain only one line of code that calls
Navigation.PushAsyncmethod onthiswith an argument of newly instantiatedNutritionView. Don't forget toawait!
- This method should contain only one line of code that calls
- Add
- Wire up buttons on our
Views/NavigationPageView.xamlto our newly created methods.- On our button with the
Textattribute with a value of{Binding Fitness}add a newClickedattribute with the value"FitnessButton_Clicked". - On our button with the
Textattribute with a value of{Binding Nutrition}add a newClickedattribute with the value"NutritionButton_Clicked".
- On our button with the
- Add click events to our
- In our
Views/NavigationPageView.xaml.csfile, wire up ourPrivacyModalto be presented when the page loads.- Create a new
privateasyncmethod with a return type ofvoidnamedPopModalthat accepts no arguments.- This method should contain only one line of code that calls
Navigation.PushModalAsyncwith arguments of a newly instantiatedPrivacyModal, andtrue. Don't forget toawait!
- This method should contain only one line of code that calls
- In our
NavigationPageView's constructor after we callInitializeComponentcall the newly createdPopModalmethod.
- Create a new
- In our
Views/NavigationPageView.xamlfile, add basic styling to dramatically improve the application's presentation.- Between our
ContentPagetags and before ourContentPage.Contenttags add an opening and closingContentPage.Resourcestags. - Between our
ContentPage.Resourcestags add an opening and closingResourceDictionarytags. - Between our
ResourceDictionarytags add a new opening and closingStyletag with the attributeTargetTypewith a value of"Button", contained between theseStyletags should be the following.- opening and closing
Settertags with the attributePropertywith a value of"Font", that haveCalibribetween these tags. - opening and closing
Settertags with the attributePropertywith a value of"BackgroundColor", that haveWhitebetween these tags. - opening and closing
Settertags with the attributePropertywith a value of"Margin", that have5between these tags. - opening and closing
Settertags with the attributePropertywith a value of"HorizontalOptions", that haveFillAndExpandbetween these tags.
- opening and closing
- After our previously added
Styletags add a new opening and closingStyletag with the attributeTargetTypewith a value of"Label", contained between theseStyletags should be the following.- opening and closing
Settertags with the attributePropertywith a value of"Font", that haveCalibribetween these tags. - opening and closing
Settertags with the attributePropertywith a value of"BackgroundColor", that haveWhitebetween these tags. - opening and closing
Settertags with the attributePropertywith a value of"HorizontalTextAlignment", that haveCenterbetween these tags. - opening and closing
Settertags with the attributePropertywith a value of"Padding", that have10between these tags. - opening and closing
Settertags with the attributePropertywith a value of"HorizontalOptions", that haveFillAndExpandbetween these tags.
- opening and closing
- Between our
Now is a good time to continue on with other Xamarin courses to expand your understanding of the Xamarin framework. You could also look at the Microsoft Azure for Developers path as Azure is often used with Xamarin applications.