Let's improve the visual appearance of our app!
- Open RecipeListView.xaml. Add the following code as part of the ContentPage properties:
BackgroundColor="{AppThemeBinding Light={StaticResource Secondary}, Dark={StaticResource Primary}}"
We are specifying the background color for the Page that changes based on whether the device is using its light or dark theme. Primary
and Secondary
are defined in Resources/Styles/Colors.xaml
, as you can see in the next picture.
- For the
Label
let's make it larger, align it to the left, make it bold, and define a text color that also changes based on the user's theme preference. Replace the previousLabel
definition with this new code:
<Label Text="Raspberry smoothie"
FontSize="Large"
HorizontalOptions="Start"
FontAttributes="Bold"
TextColor="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource Secondary}}"
/>
- What about the button? We will respect the dark/light selected theme for the background and text colors, so this is the new code for it (we chose a simpler definition this time):
<Button Text="Share"
WidthRequest="400"
TextColor="{AppThemeBinding Light=White, Dark=Black}"
BackgroundColor="{AppThemeBinding Light=Black, Dark=White}"
x:Name="ShareButton"/>
- Compile and run the application! First, let's see how our application looks like in Android:
Now, enable the Dark mode on your device. For example, here are the settings for an Android emulator.
Switch back to your app and...
...magic just happened! You can see how the page background color is different between light and dark themes, and the same happens with the label text color and the button colors (text and background).
Important: Rather than specifying a dynamic property for each View in your page, define a Style
and apply it (this maximizes code reutilization). Read more about Styles and App theme bindings.
- You can round the image corners by wrapping the
Image
into aFrame
. We add this View and move the Image definition inside as follows:
<Frame Padding="0"
CornerRadius="18"
HasShadow="False"
IsClippedToBounds="True">
<Image WidthRequest="600"
HeightRequest="400"
Source="smoothie.png"
Aspect="AspectFill"/>
</Frame>
- Run the app again, and you will see that the corners on the picture have been rounded:
Finally, let's add a popular set of icons into our app!
- Download the free and open-source Material Design Icons set by running the
npm install
command.
- Once downloaded, you will find the materialdesignicons-webfont.ttf file in the fonts folder.
- Next, you need to generate a class containing all the glyphs so you can reference your icons from C#. Visit the Icon Font to #Code website.
- Now upload the font file and you will see that a C# helper class code is shown. Copy it to the clipboard.
- Create a new folder (
Helpers
) and C# file (IconFont.cs
) in your project. Replace the default class code with the code generated from the tool (keep the default namespace from your project).
- Go to the
Fonts
folder (under theResources
folder), then add thematerialdesignicons-webfont.ttf
existing file, and make sure theBuild Action
is set toMauiFont
.
- Since you need to register the font in your MauiProgram.cs class, add the following line into the
ConfigureFonts
extension, which references the font file and sets an alias we can use in the code:
fonts.AddFont("materialdesignicons-webfont.ttf", "MaterialDesignIcons");
- Back into
RefreshingRecipes.xaml
file, add theHelpers
namespace reference at the topContentPage
definition:
xmlns:helpers="clr-namespace:RefreshingRecipes.Helpers"
- Then, we are ready to add icons into our app! Let's use one for the button that we use to share the recipe. To do that, the
FontFamily
property must reference the alias that was previously defined inMauiProgram.cs
, which isMaterialDesignIcons
.
Moreover, the button will be displayed as a circle button.
This is the new code for the button:
<Button FontFamily="MaterialDesignIcons"
Text="{x:Static helpers:IconFont.ShareVariant}"
FontSize="25"
WidthRequest="50"
HeightRequest="50"
CornerRadius="25"
TextColor="{AppThemeBinding Light=Black, Dark=White}"
BackgroundColor="{AppThemeBinding Light=White, Dark=Black}"
x:Name="ShareButton"/>
Learn more about Adding custom fonts in .NET MAUI and Creating circle buttons in XAML
- Let's test this implementation! Run the app:
Congratulations! You have finished Part 2! Let's continue and learn about the MVVM pattern in Part 3.
Thanks to Bryan Oroxon for the following implementation.
- Add a new Color definition in
Resources/Styles/Colors.xaml
:
<Color x:Key="Blue500">#3b65ff</Color>
-
Take a look at the improved
RecipeListView.xaml
, whereAppThemeBinding
is implemented for different controls. Moreover, the layout changes a bit to display theLabel
over theImage
. -
When you run the app, this is how it should look like (when Dark mode is enabled):