Xamarin中切换ListView数据源后界面不更新问题求助
Hey there! As a fellow Xamarin developer, I’ve run into this exact issue before—let’s break down how to get your ListView updating properly when switching between lists. Here are the key fixes based on your description:
1. Make Sure CustomLists Implements INotifyPropertyChanged
The biggest culprit here is usually missing property change notifications. Your ChosenList property needs to tell the UI when it’s swapped out for a different list. Update your CustomLists class to inherit from INotifyPropertyChanged and trigger the event in the ChosenList setter:
public class CustomLists : INotifyPropertyChanged { private ObservableCollection<TaskRecord> _chosenList; public ObservableCollection<TaskRecord> ChosenList { get => _chosenList; set { if (_chosenList != value) { _chosenList = value; OnPropertyChanged(nameof(ChosenList)); } } } // Implement the INotifyPropertyChanged event public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } // Your other list properties (e.g., UnfinishedTasks, FinishedTasks) go here }
2. Use ObservableCollection<T> Instead of List<T>
Plain old List<T> doesn’t notify the UI when items are added/removed. Swap all your list properties to ObservableCollection<TaskRecord>—this built-in collection automatically triggers UI updates when its contents change.
For example:
public ObservableCollection<TaskRecord> UnfinishedTasks { get; set; } = new ObservableCollection<TaskRecord>(); public ObservableCollection<TaskRecord> FinishedTasks { get; set; } = new ObservableCollection<TaskRecord>();
3. Fix the OnDelete Method & IsUnfinishedChanged_ Property
When you delete an item or toggle IsUnfinishedChanged_, you need to ensure both the collection and the property trigger notifications:
- Make sure you’re removing the
TaskRecordfrom the currentChosenList(since that’s what the UI is bound to). - Add property change notifications to
IsUnfinishedChanged_so switching its value updatesChosenListproperly.
Here’s an updated OnDelete and property example:
private bool _isUnfinishedChanged; public bool IsUnfinishedChanged_ { get => _isUnfinishedChanged; set { if (_isUnfinishedChanged != value) { _isUnfinishedChanged = value; OnPropertyChanged(nameof(IsUnfinishedChanged_)); // Swap the ChosenList when the state changes ChosenList = _isUnfinishedChanged ? UnfinishedTasks : FinishedTasks; } } } public void OnDelete(TaskRecord task) { // Remove from the currently displayed list ChosenList.Remove(task); // Toggle the task's state and move it to the other list task.IsFinished = !task.IsFinished; if (task.IsFinished) { FinishedTasks.Add(task); } else { UnfinishedTasks.Add(task); } // Optional: If you need to trigger a list switch, update IsUnfinishedChanged_ // IsUnfinishedChanged_ = !task.IsFinished; }
4. Verify Your XAML Binding
Double-check that your ListView is correctly bound to ChosenList and that your page’s BindingContext is set to an instance of CustomLists:
XAML:
<ListView ItemsSource="{Binding ChosenList}"> <!-- Your item template here --> </ListView>
Code-behind (in your page constructor):
public YourTaskPage() { InitializeComponent(); BindingContext = new CustomLists(); }
Quick Recap
The core issue is that the UI isn’t being notified when your data source (ChosenList) changes or when items are modified. By implementing INotifyPropertyChanged for your properties and using ObservableCollection<T> for your lists, you’ll ensure the UI stays in sync with your data.
内容的提问来源于stack exchange,提问作者JohnDiGriz

