由于最新的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();
    }
}

推荐答案

你的代码有两个问题.首先,您没有设置任何控件的BindingContext,因此绑定即使设置正确也无法工作.Label的定义应该如下所示:

Label lastUpdated = new() { HorizontalTextAlignment = TextAlignment.End };
lastUpdated.BindingContext = this;
lastUpdated.SetBinding(Label.TextProperty, nameof(LastUpdated));

其次,LastUpdated属性的setter是错误的.首先,设置支持字段值then call OnPropertyChanged,以便代码知道发生了更改,并且可以传播更新后的值:

public string LastUpdated
{
    get => _lastUpdated;
    set
    {
        _lastUpdated = value;
        OnPropertyChanged(nameof(LastUpdated));
    }
}

Csharp相关问答推荐

访问C#中的数据库字段时获取数据是收件箱错误-为什么?&有效,如果声明不有效

List T.AddRange在传递ConcurrentDictionary作为参数时引发ArgumentExcellent

错误401未授权ASP.NET Core.使用JWT创建身份验证/授权

什么是通过反射创建类的泛型接口方法的正确方法?

如何在C#中删除一个特殊字符,如"使用Regex"

当通过Google的Gmail Api发送邮件时,签名会产生dkim = neutral(正文散列未验证)'

如何在NodaTime中为Instant添加一年?

ASP.NET核心REST API返回一个非常大的数字

为什么EventInfo.EventHandlerType返回可为空的Type值?

ASP.NET核心MVC SqlException:违反主键约束';PK_USER';.无法在对象';数据库中插入重复的密钥.用户';

UWP应用程序try 将打包的本机.exe文件加载为C#程序集

HttpRequestMessage.SetPolicyExecutionContext不会将上下文传递给策略

DbContext-传递自定义配置选项

.NET8->;并发词典总是比普通词典快...怎么回事?[包含基准结果和代码]

为什么在使用JsonDerivedType序列化泛型时缺少$type?

Xamarin Forms应用程序中登录页面的用户名和密码编辑文本之间不需要的空格

使用C#代码和SQL SERVER中的相同证书签名会产生不同的结果

将文本从剪贴板粘贴到RichTextBox时,新文本不会在RichTextBox ForeColor中着色

我什么时候不应该在Dispose中调用EgSuppressFinalize(This)?

LINQ在GROUP BY和JOIN之后获取子列表