Controls
MAUI provides a set of cross‑platform UI controls that map to native widgets on each platform.
Button
using Microsoft.Maui.Controls;
Button myButton = new Button
{
Text = "Click Me",
BackgroundColor = Colors.Blue,
TextColor = Colors.White
};
myButton.Clicked += (s, e) =>
{
// Handle click
};
Entry
Entry usernameEntry = new Entry
{
Placeholder = "Username",
Keyboard = Keyboard.Text
};
Entry passwordEntry = new Entry
{
Placeholder = "Password",
IsPassword = true
};
Layouts
Arrange controls using layout containers.
StackLayout
StackLayout stack = new StackLayout
{
Padding = new Thickness(20),
Spacing = 10,
Children =
{
new Label { Text = "Welcome" },
new Button { Text = "Continue" }
}
};
Grid
Grid grid = new Grid
{
RowDefinitions =
{
new RowDefinition(GridLength.Auto),
new RowDefinition(GridLength.Star)
},
ColumnDefinitions =
{
new ColumnDefinition(GridLength.Star),
new ColumnDefinition(GridLength.Star)
}
};
grid.Add(new Label { Text = "Header" }, 0, 0);
grid.Add(new BoxView { Color = Colors.LightGray }, 0, 1, 2, 1);
Graphics & Drawing
Use the GraphicsView
and Canvas
APIs to render custom graphics.
GraphicsView view = new GraphicsView
{
Drawable = new MyDrawable(),
HeightRequest = 200,
WidthRequest = 200
};
class MyDrawable : IDrawable
{
public void Draw(ICanvas canvas, RectF dirtyRect)
{
canvas.FillColor = Colors.CornflowerBlue;
canvas.FillRectangle(0, 0, dirtyRect.Width, dirtyRect.Height);
}
}
App Lifecycle
Handle lifecycle events to respond to app state changes.
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new AppShell();
}
protected override void OnStart()
{
// App started
}
protected override void OnSleep()
{
// App entering background
}
protected override void OnResume()
{
// App returning to foreground
}
}
Platform‑Specific APIs
Access native features using Essentials
or platform‑specific code.
// Using Essentials to open a URL
await Browser.OpenAsync("https://learn.microsoft.com");
// Using platform-specific code (Android example)
#if ANDROID
using Android.Widget;
Toast.MakeText(Android.App.Application.Context, "Hello Android", ToastLength.Short).Show();
#endif