Formularz z walidacją
Projekt: aplikacje w stylu egzaminu INF.04 - Aplikacje mobilne: .NET MAUI krok po kroku (maui)
Typowe zadanie egzaminacyjne: kolejne warunki sprawdzają dane, a każdy błąd kończy metodę komunikatem (
return). Dopiero gdy wszystko jest dobrze, aplikacja pokazuje potwierdzenie. Contains("@") to prosta kontrola adresu e-mail.MainPage.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="MauiApp1.MainPage"
Title="Konto">
<VerticalStackLayout Padding="20" Spacing="10">
<Label Text="Rejestracja" FontSize="28" FontAttributes="Bold"/>
<Entry x:Name="LoginEntry" Placeholder="Login"/>
<Entry x:Name="EmailEntry" Placeholder="E-mail" Keyboard="Email"/>
<Entry x:Name="PasswordEntry" Placeholder="Hasło" IsPassword="True"/>
<HorizontalStackLayout Spacing="10">
<CheckBox x:Name="RulesBox"/>
<Label Text="Akceptuję regulamin" VerticalOptions="Center"/>
</HorizontalStackLayout>
<Button x:Name="RegisterBtn" Text="Zarejestruj" Clicked="OnRegisterClicked"/>
</VerticalStackLayout>
</ContentPage>MainPage.xaml.cs
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnRegisterClicked(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(LoginEntry.Text) ||
string.IsNullOrWhiteSpace(EmailEntry.Text) ||
string.IsNullOrWhiteSpace(PasswordEntry.Text))
{
await DisplayAlert("Błąd", "Uzupełnij wszystkie pola.", "OK");
return;
}
if (!EmailEntry.Text.Contains("@"))
{
await DisplayAlert("Błąd", "Niepoprawny adres e-mail.", "OK");
return;
}
if (!RulesBox.IsChecked)
{
await DisplayAlert("Błąd", "Zaakceptuj regulamin.", "OK");
return;
}
await DisplayAlert("Gotowe", $"Witaj, {LoginEntry.Text}!", "OK");
}
}
}Ładowanie interaktywnego edytora... Jeśli się nie pojawi, włącz JavaScript.
Wszystkie lekcje kursu Aplikacje mobilne: .NET MAUI krok po kroku·Zaloguj się, aby zapisać postęp na koncie.