Implementing ContentView with dependency properties to reuse UI elements
How much code is redundant in your application? Developers have been creating various techniques to avoid repeated code and yet according to different research, the percentage of repeated code may vary from 10% to 20% in an average project (https://plg.uwaterloo.ca/~migod/846/papers/bellon-tse07.pdf). In this recipe, we will see how to create reusable UI elements with ContentView
and decrease the amount of repeated code.
Getting ready
To follow the steps described in this recipe, we just need to create a blank .NET MAUI application.
The code for this recipe is available at https://github.com/PacktPublishing/.NET-MAUI-Cookbook/tree/main/Chapter03/c3-ReusableContentView.
How to do it…
Let’s create a reusable card element with a toggle button that starts editing or saves changes. We will use ContentView
with a dependency property to set card text.
Figure 3.1...