ah_Yes_NO_Tee
Implement a program where you can add name and age into a seperate list
ViewPersonPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.ViewPersonsPage">
<StackLayout>
<ListView ItemsSource="{Binding People}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Age}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
ViewPersonPage.cs
public partial class ViewPersonPage : ContentPage
{
public ViewPersonPage()
{
InitializeComponent();
BindingContext = PersonViewModel.Instance;
}
}
ViewPersonPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.ViewPersonsPage">
<StackLayout>
<ListView ItemsSource="{Binding People}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Age}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
ViewPersonPage.cs
public partial class ViewPersonPage : ContentPage
{
public ViewPersonPage()
{
InitializeComponent();
BindingContext = PersonViewModel.Instance;
}
}
8 replies
Implement a program where you can add name and age into a seperate list
Addperson.cs
public partial class AddPersonPage : ContentPage
{
private readonly PersonViewModel _personViewModel;
public AddPersonPage()
{
InitializeComponent();
_personViewModel = PersonViewModel.Instance;
}
private void OnAddPersonClicked(object sender, EventArgs e)
{
var name = NameEntry.Text;
if (int.TryParse(AgeEntry.Text, out int age))
{
var person = new Person { Name = name, Age = age };
_personViewModel.AddPerson(person);
NameEntry.Text = string.Empty;
AgeEntry.Text = string.Empty;
}
else
{
DisplayAlert("Error", "Please enter a valid age.", "OK");
}
}
}
Person.cs
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
PersonViewModel.cs
public class PersonViewModel
{
private static readonly Lazy<PersonViewModel> lazy = new Lazy<PersonViewModel>(() => new PersonViewModel());
public static PersonViewModel Instance { get { return lazy.Value; } }
public ObservableCollection<Person> People { get; private set; }
private PersonViewModel()
{
People = new ObservableCollection<Person>();
}
public void AddPerson(Person person)
{
People.Add(person);
}
}
Addperson.cs
public partial class AddPersonPage : ContentPage
{
private readonly PersonViewModel _personViewModel;
public AddPersonPage()
{
InitializeComponent();
_personViewModel = PersonViewModel.Instance;
}
private void OnAddPersonClicked(object sender, EventArgs e)
{
var name = NameEntry.Text;
if (int.TryParse(AgeEntry.Text, out int age))
{
var person = new Person { Name = name, Age = age };
_personViewModel.AddPerson(person);
NameEntry.Text = string.Empty;
AgeEntry.Text = string.Empty;
}
else
{
DisplayAlert("Error", "Please enter a valid age.", "OK");
}
}
}
Person.cs
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
PersonViewModel.cs
public class PersonViewModel
{
private static readonly Lazy<PersonViewModel> lazy = new Lazy<PersonViewModel>(() => new PersonViewModel());
public static PersonViewModel Instance { get { return lazy.Value; } }
public ObservableCollection<Person> People { get; private set; }
private PersonViewModel()
{
People = new ObservableCollection<Person>();
}
public void AddPerson(Person person)
{
People.Add(person);
}
}
8 replies
Implement a program where you can add name and age into a seperate list
MainPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage">
<StackLayout>
<Button Text="Add Person" Clicked="OnAddPersonClicked"/>
<Button Text="View Persons" Clicked="OnViewPersonsClicked"/>
</StackLayout>
</ContentPage>
MainPage.cs
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnAddPersonClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new AddPersonPage());
}
private async void OnViewPersonsClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new ViewPersonsPage());
}
}
}
AddPerson.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.AddPersonPage"
Title="AddPersonPage">
<StackLayout>
<Entry x:Name="NameEntry" Placeholder="Enter Name"/>
<Entry x:Name="AgeEntry" Placeholder="Enter Age" Keyboard="Numeric"/>
<Button Text="Add Person" Clicked="OnAddPersonClicked"/>
</StackLayout>
</ContentPage>
MainPage.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.MainPage">
<StackLayout>
<Button Text="Add Person" Clicked="OnAddPersonClicked"/>
<Button Text="View Persons" Clicked="OnViewPersonsClicked"/>
</StackLayout>
</ContentPage>
MainPage.cs
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnAddPersonClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new AddPersonPage());
}
private async void OnViewPersonsClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new ViewPersonsPage());
}
}
}
AddPerson.xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp1.AddPersonPage"
Title="AddPersonPage">
<StackLayout>
<Entry x:Name="NameEntry" Placeholder="Enter Name"/>
<Entry x:Name="AgeEntry" Placeholder="Enter Age" Keyboard="Numeric"/>
<Button Text="Add Person" Clicked="OnAddPersonClicked"/>
</StackLayout>
</ContentPage>
8 replies