I have a view model class in a folder called ViewModel. The file is called PgViewMode.cs and looks like this:
using CommunityToolkit.Mvvm.ComponentModel;
using LockAndKeyMaui.Models;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
using SQLite;
using System.ComponentModel.DataAnnotations;
using CommunityToolkit.Maui.Storage;
using Mopups.Services;
namespace LockAndKeyMaui.ViewModel
{
public partial class PgViewModel : ObservableValidator
{
public ObservableCollection<Groups> Grps { get; set; } = new ObservableCollection<Groups>();
public PgViewModel()
{
Grps = new ObservableCollection<Groups>();
}
}
}
In one of my view files, I have the XAML looking like this:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sf="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
xmlns:viewmodel="clr-namespace:LockAndKeyMaui.ViewModel"
x:DataType="viewmodel:PgViewModel"
x:Class="LockAndKeyMaui.View.PasswdInfo"
BackgroundColor="DarkGreen">
And in the code-behind of this view file the constructor looks like this:
public PasswdInfo()
{
laks = new LakFuncs();
InitializeComponent();
BindingContext = new PgViewModel();
}
Now at one point in the code-behind of the view, I have:
Grps.Clear();
foreach (var grp in grps)
{
Grps.Add(new Groups() { GrpName = grp.GrpName });
}
grplist.ItemsSource = Grps;
grplist.IsVisible = (Grps.Count > 0);
And this is where I get the errors. Grps is the definition of the ObservableCollection in the view model file. I realize it is a different file name, but shouldn’t it be recognized in some way?
1