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
XAMLInXamarinFormsTests
Project, 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
XAMLInXamarinForms
project folder and typedotnet run
. - To run the tests go into the
XAMLInXamarinFormsTests
project 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
TabbedPage
in theViews/Nutrition
folder with the nameNutritionView
.- When you create the
TabbedPage
in 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.cs
file inherits theTabbedPage
class.)- The first line should be the
xml
tag<?xml version="1.0" encoding="utf-8" ?>
. - The second line should be an opening
TabbedPage
tag 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
TabbedPage
tag.
- The first line should be the
- In our
TabbedPage
's opening tag add an attributexmlns:nutritionviews
with a value"clr-namespace:XAMLInXamarinForms.Views.Nutrition"
. - Between the
TabbedPage
tags, add opening and closingTabbedPage.Title
tags containing the valueNutrition
. - After the closing
TabbedPage.Title
tag, add a new self closing tag with the of typenutritionviews:BreakfastView
. - After the
nutritionviews:BreakfastView
tag, add a new self closing tag with the typenutritionviews:LunchView
. - After the
nutritionviews:LunchView
tag, add a new self closing tag with the typenutritionviews:DinnerView
. - After the
nutritionviews:DinnerView
tag, add a new self closing tag with the typenutritionviews:SnackView
.
- When you create the
- Create a new
CarouselPage
in theViews/Fitness
folder with the nameFitnessView
.- When you create the
CarouselPage
in 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 findCarouselPage
when creating the new page, if not useTabbedPage
and change theTabbedPage
tags toCarouselPage
and make sure theFitnessView.xaml.cs
file inherits theCarouselPage
class instead of theTabbedPage
class) - - The first line should be the
xml
tag<?xml version="1.0" encoding="utf-8" ?>
.- The second line should be an opening
CarouselPage
tag 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
CarouselPage
tag. - In our
CarouselPage
's opening tag add an attributexmlns:fitnessviews
with a value"clr-namespace:XAMLInXamarinForms.Views.Fitness"
. - Between the
CarouselPage
tags, add opening and closingCarouselPage.Title
tags containing the valueFitness
. - After the closing
CarouselPage.Title
tag, add a new self closing tag with the of typefitnessviews:SundayView
. - After the
fitnessviews:SundayView
tag, add a new self closing tag with the of typefitnessviews:MondayView
. - After the
fitnessviews:MondayView
tag, add a new self closing tag with the of typefitnessviews:TuesdayView
. - After the
fitnessviews:TuesdayView
tag, add a new self closing tag with the of typefitnessviews:WednesdayView
. - After the
fitnessviews:WednesdayView
tag, add a new self closing tag with the of typefitnessviews:ThursdayView
. - After the
fitnessviews:ThursdayView
tag, add a new self closing tag with the of typefitnessviews:FridayView
. - After the
fitnessviews:FridayView
tag, 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
FitnessView
andNutritionView
on theViews/NavigationPageView
.- Add click events to our
Views/NavigationPageView.xaml.cs
file by adding the following methods.- Add
using
directives forXAMLInXamarinForms.Views.Fitness
andXAMLInXamarinForms.Views.Nutrition
. - Create a new
private
async
method with a return type ofvoid
namedFitnessButton_Clicked
that accepts arguments of typeobject
namedsender
andEventArgs
namede
.- This method should contain only one line of code that calls
Navigation.PushAsync
method onthis
with an argument of newly instantiatedFitnessView
. Don't forget toawait
!
- This method should contain only one line of code that calls
- Create a new
private
async
method with a return type ofvoid
namedNutritionButton_Clicked
that accepts arguments of typeobject
namedsender
andEventArgs
namede
.- This method should contain only one line of code that calls
Navigation.PushAsync
method onthis
with 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.xaml
to our newly created methods.- On our button with the
Text
attribute with a value of{Binding Fitness}
add a newClicked
attribute with the value"FitnessButton_Clicked"
. - On our button with the
Text
attribute with a value of{Binding Nutrition}
add a newClicked
attribute with the value"NutritionButton_Clicked"
.
- On our button with the
- Add click events to our
- In our
Views/NavigationPageView.xaml.cs
file, wire up ourPrivacyModal
to be presented when the page loads.- Create a new
private
async
method with a return type ofvoid
namedPopModal
that accepts no arguments.- This method should contain only one line of code that calls
Navigation.PushModalAsync
with 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 callInitializeComponent
call the newly createdPopModal
method.
- Create a new
- In our
Views/NavigationPageView.xaml
file, add basic styling to dramatically improve the application's presentation.- Between our
ContentPage
tags and before ourContentPage.Content
tags add an opening and closingContentPage.Resources
tags. - Between our
ContentPage.Resources
tags add an opening and closingResourceDictionary
tags. - Between our
ResourceDictionary
tags add a new opening and closingStyle
tag with the attributeTargetType
with a value of"Button"
, contained between theseStyle
tags should be the following.- opening and closing
Setter
tags with the attributeProperty
with a value of"Font"
, that haveCalibri
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"BackgroundColor"
, that haveWhite
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"Margin"
, that have5
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"HorizontalOptions"
, that haveFillAndExpand
between these tags.
- opening and closing
- After our previously added
Style
tags add a new opening and closingStyle
tag with the attributeTargetType
with a value of"Label"
, contained between theseStyle
tags should be the following.- opening and closing
Setter
tags with the attributeProperty
with a value of"Font"
, that haveCalibri
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"BackgroundColor"
, that haveWhite
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"HorizontalTextAlignment"
, that haveCenter
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"Padding"
, that have10
between these tags. - opening and closing
Setter
tags with the attributeProperty
with a value of"HorizontalOptions"
, that haveFillAndExpand
between 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.