由于最新的CommunityToolkit存在漏洞.毛伊岛.Mvvm包与Shell一起使用时,我不使用它.因此,我正在推进用代码编写所有内容(没有Xaml).据我所知,这应该行得通.我希望上一次更新标签的文本会被更新,但它不会.有人能看到为什么不更新吗?
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();
}
}