我在 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。謝謝 !
uj5u.com熱心網友回復:
從檔案From Data Bindings to MVVM,我們知道:
ViewModel 通常實作 INotifyPropertyChanged 介面,這意味著該類在其屬性之一發生更改時會觸發 PropertyChanged 事件。Xamarin.Forms 中的資料系結機制將處理程式附加到此 PropertyChanged 事件,以便在屬性更改時通知它并使用新值更新目標。
在代碼中Account.cs ,你實作了介面INotifyPropertyChanged,但你沒有呼叫OnPropertyChanged。所以即使值改變了,UI也不會重繪 。
可以參考以下代碼:
public class Account: INotifyPropertyChanged
{
string _student_fullname;
public string student_fullname
{
set { SetProperty(ref _student_fullname, value); }
get { return _student_fullname; }
}
private bool _isVisible;
public bool isVisible
{
set { SetProperty(ref _isVisible, value); }
get { return _isVisible; }
}
// you can modify other codes to trigger `SetProperty` just as above code
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
筆記:
您可以修改類中的其他欄位Account以觸??發SetProperty,就像上面的代碼一樣。和班級一樣AccountGroup.cs
此外,您可以為您的 定義一個全域變數users.ItemsSource,例如:
public ObservableCollection<AccountGroup> items { get; private set; }
并按如下方式初始化:
items = new ObservableCollection<AccountGroup>();
ItemsSource 的初始宣告:
users.ItemsSource = items;
In this condition,if you want the collectionview to automatically update as items are added, removed and changed in the underlying list, we'll need to use an ObservableCollection.And we don't need the following code:
users.ItemsSource = null;
users.ItemsSource = new System.Collections.ObjectModel.ObservableCollection<AccountGroup>(Accounts);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313140.html
標籤:C# 沙马林 xamarin.forms 数据绑定
