I am calling a page from my main page and I am using QueryProperty to pass a string. I want to evaluate this string and create a different list of objects depending on which string was passed so that I can reuse this same page for different things.
The problem is that when EnterValueViewModel runs and I want to check my passed string, the value is still null. How can I run the if statement after the value has been assigned?
[QueryProperty("TypeOfCaller", "TypeOfCaller")]
public partial class EnterValueViewModel : ObservableObject
{
[ObservableProperty]
string typeOfCaller;
[ObservableProperty]
ObservableCollection<string> listOfObjects;
public EnterValueViewModel()
{
if (typeOfCaller == "PermitHolder")
{
listOfObjects = new ObservableCollection<string>(PermitHolderRepository.GetPermitHolders().Select(p => p.Name));
}
else if (typeOfCaller == "Company")
{
listOfObjects = new ObservableCollection<string>(CompanyRepository.GetCompanies().Select(p => p.Name));
}
else if (typeOfCaller == "Facility")
{
listOfObjects = new ObservableCollection<string>(FacilityRepository.GetFacilities().Select(p => p.Name));
}
}
I tried putting the if statement inside a method, but i don't know when to call it. I need an event that executes after the page is loaded but before the user can interact with it.
To execute code when an observable property changes, use one of the auto-generated methods for the property.
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty
Also, why are you using an ObservableCollection if you're going to recreate it every time? Just create the collection once and update it as required.