嵌套对象未更新绑定 Xamarin.Forms

Nested object is not updating Binding Xamarin.Forms

我想制作带有姓名和详细信息的卡片。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:vm="clr-namespace:MinerController.ViewModels" 
             xmlns:cards="clr-namespace:Syncfusion.XForms.Cards;assembly=Syncfusion.Cards.XForms"
             x:Class="MinerController.Views.Dashboard"
             Appearing="ContentPage_Appearing">
    <ContentPage.BindingContext>
        <vm:DashboardVM />
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <Label Text="Datos de las pools" FontSize="Title" HorizontalTextAlignment="Center"  />
            <StackLayout BindableLayout.ItemsSource="{Binding UserInfo}" BindableLayout.EmptyView="No data." Orientation="Horizontal">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <cards:SfCardView BackgroundColor="SlateGray" HasShadow="True" WidthRequest="100" Margin="5" Padding="5">
                        <Grid>
                            <Label Grid.Row="0" Text="{Binding Name}"/>
                            <Label Grid.Row="1" Text="{Binding LastUpdateHumanized}" />
                            <StackLayout Grid.Row="2" BindableLayout.ItemsSource="{Binding AdditionalInfo}" BindableLayout.EmptyView="No data.">
                                <BindableLayout.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Label Grid.Row="0" Text="{Binding Address}" />
                                            <Label Grid.Row="1" Text="{Binding Availability}" /> 
                                        </Grid>
                                    </DataTemplate>
                                </BindableLayout.ItemTemplate>
                            </StackLayout>
                        </Grid>
                        </cards:SfCardView>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

视图模型:

internal class DashboardVM : BaseViewModel
{
  private InternalService internalService;
  public ObservableCollection<UserInfo> UserInfo { get; private set; }
  public DashboardVM()
  {
    UserInfo = new ObservableCollection<UserInfo>();
    internalService = new InternalService();
    if (ConfigService.Configuration != null)
    {
      foreach(var person in ConfigService.Configuration.Persons)
      {
        UserInfo.Add(new UserInfo() { Name = person.Name, AdditionalsInfo = new ObservableCollection<AdditionalInfo>() });
        foreach(var additionalinfo in person.AdditionalsInfo)
        {
          UserInfo.Last().AdditionalsInfo.Add(new AdditionalInfo() { address = person.Address, Availiability = person.Availability });
        }
      }
      _ = TimedUpdate();
    }
  }
  private async Task TimedUpdate()
  {
    while (true)
    {
      await UpdateInfo();
      await Task.Delay(60000);
    }
  }
  public async Task UpdateInfo()
  {
    foreach (var user in UserInfo)
    {
      foreach (var additionalInfo in user.AdditionalsInfo)
      {
        additionalInfo.Availability = await internalservice.GetAvailability(user.Name, additionalInfo.Address);
      }
      user.LastUpdate = DateTime.Now;
    }
    OnPropertyChanged(nameof(UserInfo));
  }
}

型号:

internal class UserInfo
{
  public string Name { get; set; }
  public ObservableCollection<AdditionalInfo> AdditionalsInfo { get; set; }
  public DateTime LastUpdate { get; set; }
  public string LastUpdateHumanized { get { return LastUpdate.Humanize(); } }

}
internal class AdditionalInfo
{
  public string Address { get; set; }
  public string Availability { get; set; }
}

这段代码的结果如下:当我第一次加载时,它从配置文件中获取所有数据,然后,我需要定期更新用户的“可用性”,但是当我更新对象时,它不会在屏幕上更新。也许绑定被忽略了,但为什么呢?或者问题是因为嵌套对象?我很好?在帮助我之前,请阅读以下内容:我不想要解决方法,因为我仍然需要用其他东西填充“仪表板”,并在 mvvm 之外制作一些东西,我认为这很丑陋。

谢谢!

编辑:对不起,我忘了显示 BaseViewModel:

internal class BaseViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

您的模型对象需要实现 INotifyPropertyChanged 才能通知对 UI 的更改。

您的对象不会被通知它已更改。要解决您的问题,您可以创建一个继承自 Inofify属性Change 的根 class。

public class Root : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        internal void RaisePropertyChanged([CallerMemberName]string propertyName ="")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        }

    }

然后从此 class 继承任何模型以实现 属性 更改的事件。

internal class AdditionalInfo : Root
{
  public string Address { get; set; }
  private string availability;
  
  public string Availability
  { 
  get{ return availability;}
  set
     { availability= value;  
       RaisePropertyChanged();
     } 
  }
}