Due to a bug with the latest CommunityToolkit.Maui.Mvvm package when used with Shell, I'm not using it. So I'm pushing ahead just writing everything in code (no Xaml). From what I can tell, this should work. I'm expecting the text of the last updated label to be updated, but it doesn't. Can anyone see why not?
namespace MyMauiApp;
public class DashboardPage : ContentPage
{
private string _lastUpdated;
private bool _loading;
public DashboardPage()
{
LoadData = new AsyncCommand(SetData);
RefreshView refreshView = new();
refreshView.RefreshColor = ColourConstants.DesaturatedDark;
refreshView.Bind(RefreshView.IsRefreshingProperty, nameof(_loading));
refreshView.Bind(RefreshView.CommandProperty, nameof(LoadData));
ScrollView scrollView = new();
var dashgrid = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = new GridLength(0, GridUnitType.Auto) },
},
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(0, GridUnitType.Auto) },
new ColumnDefinition { Width = new GridLength(0, GridUnitType.Auto) },
},
}.CenterVertical();
dashgrid.Add(new Label { Text = "Last Updated:" }, 0, 0);
Label lastUpdated = new() { HorizontalTextAlignment = TextAlignment.End };
lastUpdated.SetBinding(Label.TextProperty, nameof(LastUpdated));
dashgrid.Add(lastUpdated, 1, 0);
scrollView.Content = dashgrid;
refreshView.Content = scrollView;
Content = refreshView;
}
public string LastUpdated
{
get => _lastUpdated;
set
{
OnPropertyChanged(nameof(LastUpdated));
_lastUpdated = value;
}
}
public AsyncCommand LoadData { get; }
private async Task SetData()
{
try
{
LastUpdated = DateTime.Now.ToString("dd/MM/yy hh:mm tt");
}
catch (Exception)
{
Debug.WriteLine("Failed to set data");
}
finally
{
_loading = false;
}
}
protected override void OnAppearing()
{
base.OnAppearing();
_ = SetData();
}
}