Bài này dựng giao diện MAUI với các layout, control, CollectionView và điều hướng giữa các trang bằng Shell.
Bạn sẽ học được
- VerticalStackLayout, Grid, FlexLayout
- Entry, Button, Picker, Switch
- CollectionView hiển thị danh sách
- Shell: tab, điều hướng, truyền tham số
Cơ sở lý thuyết
Giao diện MAUI gồm các trang (ContentPage, TabbedPage, FlyoutPage), layout (VerticalStackLayout, Grid, FlexLayout) và view (Label, Entry, Button, CollectionView). CollectionView hiển thị danh sách với ảo hóa, bố cục dọc, ngang hoặc lưới và DataTemplate mô tả giao diện mỗi phần tử.
- Shell cung cấp cấu trúc điều hướng tổng thể (tab, menu trượt) và điều hướng dựa trên URI: GoToAsync("chitiet?ma=SV01").
- Tham số truyền qua chuỗi truy vấn được nhận bằng [QueryProperty] hoặc IQueryAttributable.
- Thiết kế đáp ứng: OnPlatform, OnIdiom cho phép giá trị khác nhau theo nền tảng, loại thiết bị; VisualStateManager đổi bố cục theo kích thước cửa sổ.
- Nguyên tắc thiết kế di động: vùng chạm đủ lớn, nội dung quan trọng ở vùng ngón cái dễ chạm.
Danh sách
<CollectionView ItemsSource="{Binding DanhSach}" SelectionMode="Single"
SelectionChangedCommand="{Binding ChonCommand}"
SelectionChangedCommandParameter="{Binding SelectedItem, Source={RelativeSource Self}}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Border Padding="12" Margin="8,4" StrokeShape="RoundRectangle 8">
<VerticalStackLayout>
<Label Text="{Binding Ten}" FontSize="18" FontAttributes="Bold"/>
<Label Text="{Binding Diem, StringFormat='Điểm: {0}'}"/>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>Shell
<!-- AppShell.xaml -->
<TabBar>
<ShellContent Title="Danh sách" ContentTemplate="{DataTemplate local:DanhSachPage}"/>
<ShellContent Title="Thêm" ContentTemplate="{DataTemplate local:ThemPage}"/>
</TabBar>
// Điều hướng có tham số
Routing.RegisterRoute("chitiet", typeof(ChiTietPage));
await Shell.Current.GoToAsync("chitiet?ma=SV01");Ứng dụng thực tế
Bài toán: ứng dụng kiểm kê tài sản của trường: trên điện thoại hiển thị một cột, trên máy tính bảng và Windows hiển thị lưới 3 cột, cùng một mã XAML:
<CollectionView ItemsSource="{Binding TaiSans}">
<CollectionView.ItemsLayout>
<GridItemsLayout Orientation="Vertical"
Span="{OnIdiom Phone=1, Tablet=3, Desktop=3}"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Border Padding="12" Margin="6">
<VerticalStackLayout>
<Label Text="{Binding Ten}" FontAttributes="Bold"/>
<Label Text="{Binding Phong, StringFormat='Phòng: {0}'}"/>
</VerticalStackLayout>
</Border>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>Thực hành
- Làm 2 tab Danh sách và Thêm mới.
- Bấm một sinh viên mở trang chi tiết.
- Thử giao diện trên Windows và Android.
Tóm tắt bài học
- CollectionView cho danh sách.
- Shell quản lý tab và điều hướng.
- Binding dùng như WPF.

