【发布时间】:2023-03-09 20:03:01
【问题描述】:
我在 XAML 中有一个绑定,它作为 ItemsSource 链接到对象列表。在 Android 上,当我更新 ItemsSource 时,它会成功更新 XAML 中的绑定。
但是在 iOS 上,由于某种原因,这不起作用。谁能提出原因?
ItemsSource 的初始声明:
users.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<AccountGroup>(Accounts);
更新 ItemsSource 的 CS 代码:
users.ItemsSource = null;
users.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<AccountGroup>(Accounts);
这是帐户组对象:
public class Account : INotifyPropertyChanged
{
public string student_unique_id { get; set; }
public string student_fullname { get; set; }
public string organisation_id { get; set; }
public string organisation_title { get; set; }
public string student_token { get;set;}
public string reg_branding_url { get;set;}
public string tint_colour { get;set;}
public string font_colour { get;set;}
public string topup_product_id { get;set;}
public bool isVisible { get; set; }
public string student_initial
{
get
{
return student_fullname[0].ToString();
}
}
public string school_image
{
get
{
return $"{Constants.apiBaseUrl}store/images/uploaded/mobile/{reg_branding_url}";
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class AccountGroup : List<Account>, INotifyPropertyChanged
{
public string organisation_title { get; set; }
public string school_image { get; set; }
public string tint_colour { get; set; }
public string font_colour { get; set; }
public AccountGroup(string orgTitle, string orgImage, string orgTint, string orgFont, List<Account> accounts) : base(accounts)
{
organisation_title = orgTitle;
school_image = orgImage;
tint_colour = orgTint;
font_colour = orgFont;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
这是具有 IsVisible 属性的 XAML:
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame IsVisible="{Binding isVisible}" IsEnabled="False" HasShadow="True" BackgroundColor="White" Padding="0">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<behaviors:Expander x:Name="MainExpander" CollapseAnimationLength="500" IsExpanded="False" IsVisible="True" >
谁能建议如何解决这个问题?看起来很奇怪,它适用于 Android 而不是 iOS。谢谢!
【问题讨论】:
-
只有IsVisible没有更新吗?其他属性是否更新?您是否尝试过在 MainThread 上强制更新?
-
您正在实施
INotifyPropertyChanged,但您从未调用OnPropertyChanged。奇怪的是它可以在 Android 上运行。此外,将 ItemSource 设置为 Null 并设置一个新的也不是一个好习惯 -
Jason - 似乎没有任何属性更新。我在主线程上试过了。
标签: c# xamarin xamarin.forms data-binding