.NET MAUI 标签绑定未在 C# 中更新 - 不是 MVVM

德沃努托

由于与 Shell 一起使用时最新的 CommunityToolkit.Maui.Mvvm 包存在错误,我没有使用它。所以我只是在代码中编写所有内容(没有 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();
    }
}
r2d2rigo

您的代码有两个问题。首先,您没有设置BindingContext任何控件,因此即使设置正确,绑定也不会起作用。你的定义Label应该是这样的:

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

其次,属性的设置器LastUpdated是错误的。首先,您设置支持字段值,然后调用OnPropertyChanged以便代码知道发生了更改并且可以传播更新的值:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

.NET Maui MVVM 选择器绑定 SelectedIndexChanged 事件

.net maui MVVM 从 CollectionView 绑定 SelectedItemCommand 和 SelectedItemParameter

如何使用 c# 而不是 xaml 创建 .Net Maui ContentPage?

.net maui 无法获取 Observable 集合中的更新字段以在绑定集合视图中更新

.net maui preview 10 不是單個項目

如何在 .NET MAUI 中隐藏 ImageButton

在 .NET MAUI/Xamarin.Forms 中使用 MVVM 的手势识别器

.Net Maui MVVM - 在页面/视图打开时填充 CollectionView 的最佳方法是什么?

为什么动态更改的数据未在MVVM绑定上更新

.net Maui 数据绑定到外壳弹出项 IsVisible 属性

.NET MAUI 中是否有保存文件的对话框?

如何在 .NET MAUI ViewModel 中显示警报

.NET MAUI/Xamarin.Forms 中的实体框架核心

C# WPF MVVM 使用从 textBox 计算的数据更新标签

为什么 MAUI 中的 Switch 和 ListView 控件不使用 2 路绑定更新?

绑定到WrapPanel,但未在WPF C#中更新

在MVVM结构之后,如何在WPF中绑定方法而不是变量?

如何在 C# (MVVM) 中绑定图像源

MAUI .NET 设置窗口大小

绑定属性只在代码中更新,而不是在 UI 中更新

从异步任务激发的事件中更新MVVM绑定

Xamarin标签在Mvvm listview中初始后未更新

从另一个更新WPF窗口(不是MainWindow)中的datagrid。*不使用MVVM *

MVVM WPF中的数据绑定

.Net MAUI 是否与 .NET Standard 2.0 兼容?

如何在 .net MAUI 中删除不需要编译的平台?

.net maui 中的自定义控件和渲染器会发生什么?

标签未在Kivy中更新

WPF MVVM TextBox即时绑定属性更新