嵌套对象未更新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>

ViewModel:

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的更改。

您的对象不会收到更改的通知。为了解决您的问题,您可以创建一个从InofiyPropertyChange继承的根类。

public class Root : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
internal void RaisePropertyChanged([CallerMemberName]string propertyName ="")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

然后从这个类继承任何模型来实现Property Changed Event。

internal class AdditionalInfo : Root
{
public string Address { get; set; }
private string availability;

public string Availability
{ 
get{ return availability;}
set
{ availability= value;  
RaisePropertyChanged();
} 
}
}

相关内容

  • 没有找到相关文章

最新更新