Skip to content

Instantly share code, notes, and snippets.

@androidmads
Created October 28, 2023 18:06
Show Gist options
  • Save androidmads/354012432f9ac014551d5cb774c48e3b to your computer and use it in GitHub Desktop.
Save androidmads/354012432f9ac014551d5cb774c48e3b to your computer and use it in GitHub Desktop.
.NET MAUI - MVVM with MVVM toolkit sample
<?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="MauiMVVM.Views.ItemEntryPage"
Title="Item Entry">
<StackLayout Margin="20"
Spacing="10">
<VerticalStackLayout>
<Label Text="Name:"
FontSize="16"/>
<Entry Text="{Binding Name}"
Placeholder="Item Name"/>
</VerticalStackLayout>
<VerticalStackLayout>
<Label Text="Description:"
FontSize="16"/>
<Entry Text="{Binding Description}"
Placeholder="Item Description"/>
</VerticalStackLayout>
<Button x:Name="btn_save"
Text="Save"
Command="{Binding SaveCommand}"/>
</StackLayout>
</ContentPage>
using MauiMVVM.ViewModels;
namespace MauiMVVM.Views;
public partial class ItemEntryPage : ContentPage
{
public ItemEntryPage()
{
InitializeComponent();
BindingContext = new ItemEntryPageModel();
}
}
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
namespace MauiMVVM.ViewModels;
public partial class ItemEntryPageModel : ObservableObject
{
[ObservableProperty]
private int _id;
[ObservableProperty]
private string _name;
[ObservableProperty]
private string _description;
[ICommand]
public async void Save()
{
await Application.Current.MainPage.DisplayAlert("MAUI MVVM Sample", "Item Saved Successfully", "OK");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment