Skip to the content

What is .NET MAUI?

.NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps with C# and XAML.

Using .NET MAUI, you can develop apps that can run on Android, iOS, macOS, and Windows from a single shared code-base.

.NET MAUI supported platforms.

.NET MAUI is open-source and is the evolution of Xamarin.Forms, extended from mobile to desktop scenarios, with UI controls rebuilt from the ground up for performance and extensibility. If you've previously used Xamarin.Forms to build cross-platform user interfaces, you'll notice many similarities with .NET MAUI. However, there are also some differences. Using .NET MAUI, you can create multi-platform apps using a single project, but you can add platform-specific source code and resources if necessary. One of the key aims of .NET MAUI is to enable you to implement as much of your app logic and UI layout as possible in a single code-base.

Xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="DemoNativeApp.MainPage">

<StackLayout Padding="35">
<Label Text="Name*" FontSize="Large"/>
<Entry x:Name="name" Placeholder="name" FontSize="Large" />
<Label Text="Email" FontSize="Large"/>
<Entry x:Name="email" Placeholder="email" FontSize="Large"/>
<Label Text="PhoneNumber" FontSize="Large"/>
<Entry x:Name="phonenumber" Placeholder="phonenumber" FontSize="Large"/>
<Label Text="Date" FontSize="Large"/>
<Entry x:Name="dateofbirth" Placeholder="Select date" IsReadOnly="True" />
<DatePicker x:Name="DateOfBirthPicker" DateSelected="OnDateSelected" />
<Label Text="Description" FontSize="Large"/>
<Entry x:Name="description" Placeholder="description" FontSize="Large"/>
<Label x:Name="ResultLabel" Text="Stored Data" FontSize="16" />
<Button Text="Submit" Clicked="SubmitButton_Clicked" FontSize="Title" HorizontalOptions="Start" VerticalOptions="Start"/>
<Button Text="Delete" Clicked="DeleteButton_Clicked" WidthRequest="100"/>
<Button Text="Clear" Clicked="ClearButton_Clicked" WidthRequest="100" />
<Button Text="Show Data" Clicked="ShowButton_CLicked"/>

</StackLayout>

</ContentPage>

-

create xaml.cs file.

private Database _database;
public ObservableCollection<DemoNativeApp.Model.Model> People { get; set; }

public MainPage()
{
InitializeComponent();
string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "people.db3");
_database = new Database(dbPath);
People = new ObservableCollection<DemoNativeApp.Model.Model>();
LoadPeople(); // Load data when the page is initialized
BindingContext = this;

}
private void OnDateSelected(object sender, DateChangedEventArgs e)
{
dateofbirth.Text = e.NewDate.ToString("d");
}
private async void SubmitButton_Clicked(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(name.Text)||string.IsNullOrEmpty(email.Text))
{
await DisplayAlert("Error", "Please Fill All Field", "Ok");
return;
}

else
{ DateTime selectedDate = DateOfBirthPicker.Date;
var person = new DemoNativeApp.Model.Model
{

Name = name.Text,
Phonenumber = phonenumber.Text,
Email = email.Text,
Description = description.Text,
Date= selectedDate.ToString("d"),


};


await _database.SaveModelAsync(person);


//await Navigation.PushAsync(new DisplayData());
//await DisplayAlert("Success", "Person saved successfully", "OK");
//ClearForm();
// Load your data here
List<Model.Model> loadedData = await LoadPeople();

// Navigate to Page2 and pass the loaded data as a parameter
await Navigation.PushAsync(new DisplayData(loadedData));
await LoadPeople();
ClearForm();
}

}
private async void ClearButton_Clicked(object sender,EventArgs e)
{
ClearForm();
}
private async void ShowButton_CLicked(object sender,EventArgs e)
{
List<Model.Model> loadedData = await LoadPeople();
await Navigation.PushAsync(new DisplayData(loadedData));

}
private async void DeleteButton_Clicked(object sender, EventArgs e )
{
var delete = await _database.GetPeopleAsync();
foreach (var dlt in delete)
{
await _database.DeleteModelAsync(dlt);
}

await DisplayAlert("Success", "Data Successfully Deleted", "Ok");
LoadPeople();

}


private async Task<List<Model.Model>> LoadPeople()
{
var people = await _database.GetPeopleAsync();
People.Clear();
foreach (var person in people)
{
People.Add(person);

}
return people;

}

private void ClearForm()
{
name.Text = string.Empty;
phonenumber.Text = string.Empty;
email.Text = string.Empty;
description.Text = string.Empty;
dateofbirth.Text = string.Empty;
}
}

}

 

First Database Connectivity
=>Submit Button Event()

in this event fetch data from the xaml file.and after validation,data store into database.

and then,get data from database and navigate to second page displaydata.cs.

Clear Button Event()

call clearform function in this event .

which clear form data on button click.

Show Button Event ()

on show button click navigate  displaydata(xaml) page.

Delete Button Event()

delete all records from the database.

DisplayData.xaml is new content page like mainpage.xaml and mainpage.xaml.cs.
if you want to add this page in solution explorer,right click on the project name then add.
after click on the add button show list of differnt page.
you click on the any page which you want to add.

 

 

 

DisplayData.xaml:
namespace DemoNativeApp;
public partial class DisplayData : ContentPage
{
public DisplayData(List<Model.Model>models)
{
InitializeComponent();
BindingContext = models;
}
private async void OnBackButtonClicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
}
Fetch data from xaml.cs file and display in displaydata.xaml(view) page.

DisplayData() constructor get data from xaml.cs file.

and show on view.

onbackbuttonclicked event used for back on to home page.

 

 

Built-in Components

  1. Controls: MAUI provides a variety of built-in controls for creating user interfaces. These include:

    • Button: For creating clickable buttons.
    • Label: For displaying text.
    • Entry: For single-line text input.
    • Editor: For multi-line text input.
    • Image: For displaying images.
    • CollectionView: For displaying a collection of data.
    • ListView: For displaying a scrollable list of data.
    • Picker: For selecting a value from a list.
    • Slider: For selecting a value from a continuous range.
    • Switch: For toggling between on and off states.
  2. Layouts: MAUI provides several layout components to organize UI elements:

    • StackLayout: Stacks child elements vertically or horizontally.
    • Grid: Organizes elements in a grid with rows and columns.
    • AbsoluteLayout: Positions elements using absolute coordinates.
    • FlexLayout: Aligns elements based on the flexible box model.
  3. Graphics and Drawing: MAUI includes tools for drawing shapes, paths, and other graphical elements.

    • Shapes: Ellipse, Line, Polygon, Polyline, Rectangle.
    • Paths: For defining complex shapes using path data.
  4. Navigation: Tools for navigating between different pages and views within the application.

    • NavigationPage: Provides a navigation stack for navigating between pages.
    • Shell: Simplifies the creation of complex navigation structures and applications with flyouts and tabs.
  5. Data Binding and MVVM: MAUI supports data binding and the Model-View-ViewModel (MVVM) pattern to separate UI logic from business logic.

    • Binding: Mechanism to bind UI elements to data sources.
    • Command: Mechanism for handling user interactions through commands in the ViewModel.

Third-Party Components

Third-party components are additional libraries and tools provided by the community or commercial vendors to extend the functionality of MAUI applications. These components can include:

  1. UI Components: Enhanced or custom controls that provide additional functionality or better performance.

    • Syncfusion: Offers a wide range of UI components like charts, data grids, and schedulers.
    • Telerik: Provides UI components such as charts, calendars, and data visualization tools.
  2. Libraries: Additional libraries that provide functionalities not covered by the built-in components.

    • Prism: A framework for building maintainable and testable XAML applications using MVVM.
    • ReactiveUI: A framework for building reactive applications using the MVVM pattern.
  3. Plugins: Extensions that provide specific functionalities such as accessing device features.

    • Xamarin.Essentials: Provides cross-platform APIs for device features like accelerometer, geolocation, and secure storage.
    • Community Toolkit: A collection of reusable elements for application development including behaviors, converters, and effects.
  4. Themes and Templates: Pre-designed themes and templates to enhance the visual appearance of applications.

    • Grial UI Kit: A collection of pre-designed screens and templates for building visually appealing applications.

Usage

To use built-in components, developers simply reference them directly in their XAML or C# code. For third-party components, developers typically need to install the corresponding NuGet packages and include the necessary namespaces in their project.

 

About the author

BJ Patel

BJ Patel is an expert user of Umbraco. Always keen to share hints and tips on getting the best out of Umbraco.

comments powered by Disqus

Join Our Community

This is a promo pod

Join Our Community Become a part of our developer community. Share your projects, get feedback, and collaborate with like-minded individuals.

Your contributions help us continue creating valuable content. Consider supporting us by sharing our content.

Junagadh, Gujarat

Support Us.