Reputation: 2908
.net 7
CommunityToolKit.mvvm 8.1.0
I've got many pages working great in a .net maui application. In working on wiring another new page, I've encountered an odd issue. My viewmodel appears to be the same as the others that I have which are working.
First the data object:
public class allRegsGroupped
{
public Guid? visualId { get; set; }
public Guid? eventId { get; set; }
public string? groupName { get; set; }
}
In the view model I have this:
using CommunityToolkit.Mvvm.ComponentModel;
using Newtonsoft.Json;
using SharedModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App.ViewModels
{
public partial class ManageEventViewModel : ObservableObject, IQueryAttributable
{
[ObservableProperty]
ObservableCollection<allRegsGroupped> allGroupRegs;
[ObservableProperty]
allRegsGroupped test;
EventPublicID _selectedEvent;
public ManageEventViewModel() { }
public void ApplyQueryAttributes(IDictionary<string, object> query)
{
_selectedEvent = query["selectedEvent"] as EventPublicID;
}
public async Task LoadAsync()
{
App.Globals.SetHttpClient();
try
{
EventPublicID epi = _selectedEvent;
var json = JsonConvert.SerializeObject(epi, Formatting.Indented);
var content = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
var response = await App.Globals.MyHttpClient.PostAsync(App.Globals.APIURL + "getAllRegistrationsForEvent", content);
var allGroupsResponse = response.Content.ReadAsStringAsync().Result;
if (allGroupsResponse != null)
{
List<allRegsGroupped> listOfGroups = JsonConvert.DeserializeObject<List<allRegsGroupped>>(allGroupsResponse);
if (listOfGroups != null)
{
AllGroupRegs = new ObservableCollection<allRegsGroupped>(listOfGroups.OrderBy(x => x.groupName));
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + "\r\b" + ex.StackTrace);
}
}
...
My XAML looks like this:
<?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="App.Pages.ManageEventPage"
Title="Manage Event"
xmlns:viewmodel="clr-namespace:App.ViewModels"
xmlns:dm="clr-namespace:SharedModels;assembly=SharedModels"
x:DataType="viewmodel:ManageEventViewModel"
NavigatedTo="ContentPage_NavigatedTo"
>
<VerticalStackLayout>
<CollectionView ItemsSource="{Binding AllGroupRegs}">
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout>
<Label Text="{Binding groupName}" FontAttributes="Bold" />
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</ContentPage>
In the XAML page, if I leave this line in:
<Label Text="{Binding groupName}" FontAttributes="Bold" />
During compile I get:
XFC0045 Binding: Property "groupName" not found on "App.ViewModels.ManageEventViewModel".
groupName is there...
If I remove it, I got an even a weirder behavior.
Originally, I was getting a null reference error when the LoadAsync ran on this line:
AllGroupRegs = new ObservableCollection<allRegsGroupped>(listOfGroups.OrderBy(x => x.groupName));
Even though the data did come down and was in the "listOfGroups". As an attempt to fix it, I re-created the page and viewmodel. After I did that, this section of code started to work. The viewmodel was copied from the old one to the new one so it was the same except for the viewmodel declaration.
Another odd thing I've been experiencing. Occasionally (but somewhat frequently), a viewmodel will lose its mind and throw a whole bunch of compile errors on observable properties which are no longer able to be resolved. It always happens to a viewmodel which is open during run or compile, never to a viewmodel which is not opened. This is fixed by exiting VS2022 and going back into it. I am beginning to wonder if I'm hitting a bug of some sort in CommunityToolkit.
Upvotes: 1
Views: 1520
Reputation: 2908
Humble pie...
The issue was I didn't have the datamodel specified in the datatemplate in the XAML file.
So this:
<DataTemplate>
to this:
<DataTemplate x:DataType="dm:allRegsGroupped">
The other errors persist, but I'm betting they are due to a bug in the framework.
Upvotes: 0
Reputation: 91
For the compile time errors a work around is to frequently delete the content of obj and bin folders and reload the project. Most of the time you need to exit VS in order to delete the files as they get locked if Visual Studio is open.
As for [OBSERVABLEPROPERTY] I still have code that works and compiles but visual studio gives error on in error list. It comes and goes depending on who know what. But deleting contents of above folders and restarting visual studio fixes a ton of issues.
Upvotes: 0