Over the last week we’ve had a look at the basics of C# programming, explored XAML, built our first .NET MAUI app and played around with UI controls. Now we will have a look to see how the frontend UI designed in XAML is connected to the backend logic written in C#.
Making buttons do things: Clicks and Actions
Clicking buttons makes your app interactive. You’ve seen how to design buttons in XAML, now let’s make them do stuff when clicked.
Let’s create another simple button in MainPage.xaml:
And then the code-behind in MainPage.xaml.cs
Now every time someone taps the “Click me” button, your app will do something and this is what it would then look like:
We can also bind text. Let adjust the label in XAML to:
And then in the C# code add:
Now changes in your code will automatically show up in your app.
In this post we’ve connected the dots between design you create in XAML and the logic you write in C# for your .NET MAUI app. We've looked at clicking buttons, talking to your app and binding your code and design to start you on your journey into the world of .NET MAUI.
More info:
How to Learn C# as a Beginner in 2023 hackr.io
C# | Modern, open-source programming language for .NET microsoft.com
C Sharp (programming language) Wikipedia
References:
Get Started with C# w3schools.com
A tour of C# - Overview - C# | Microsoft Learn