Implement a program where you can add name and age into a seperate list

I want to have two form pages apart from the main page where you can maneuver two the page with the name and age input and to the page with the inputted name and age. The problem is that the list is not getting updated. The solution is using singleton but I have no clue how to implement that.
4 Replies
Mayor McCheese
Mayor McCheese3mo ago
$code
MODiX
MODiX3mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Mayor McCheese
Mayor McCheese3mo ago
What have you tried so far?
ah_Yes_NO_Tee
ah_Yes_NO_Tee3mo ago
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>
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);
}
}
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;
}
}
that's the code but it isnt actually working and somehow the list is not even showing at all
Want results from more Discord servers?
Add your server