0
我有一個分組Xamarin ListView,並發現它更新ListView中的值麻煩。第二次刷新分組listview崩潰
ViewModel;
public class MainViewModel
{
private ObservableCollection<ObservableGroupCollection<string, TaskItem>> _userTasksGrouped;
public ObservableCollection<ObservableGroupCollection<string, TaskItem>> UserTasksGrouped
{
get => _userTasksGrouped;
set
{
_userTasksGrouped = value;
OnPropertyChanged();
}
}
private void PopulateMyTasks()
{
UserTasks.Clear();
foreach (var task in MyTasks)
{
UserTasks.Add(new TaskItem(task));
}
DoTaskGrouping();
}
private void DoTaskGrouping()
{
UserTasksGrouped.Clear();
var groupedData =
UserTasks
.OrderBy(ut => ut.Name)
.GroupBy(ut => ut.StateEnum.ToString())
.Select(ut => new ObservableGroupCollection<string, TaskItem>(ut))
.ToList();
foreach (var group in groupedData)
UserTasksGrouped.Add(group);
}
public void UpdateTaskItem(TaskChanged taskChanged)
{
// update listing
var usertask = UserTasksGrouped.First(utg => utg.Any(ut => ut.Task.Id == taskChanged.Task.Id))
.First(t => t.Task.Id == taskChanged.Task.Id);
if (usertask != null)
{
usertask.Task = taskChanged.Task;
}
else
{
// something else
}
DoTaskGrouping();
}
public MainViewModel()
{
UserTasks = new ObservableCollection<TaskItem>();
UserTasksGrouped = new ObservableCollection<ObservableGroupCollection<string, TaskItem>>();
}
}
XAML;
<ListView
IsGroupingEnabled="true"
ItemsSource="{Binding UserTasksGrouped}"
GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="50*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<BoxView
Grid.Row="0" Grid.RowSpan="2"
Grid.Column="0" Grid.ColumnSpan="2"
MinimumHeightRequest="16"
MinimumWidthRequest="16"
Margin="0"
Color="{Binding StatusColour}" />
<Label
Grid.Row="0" Grid.Column="2"
Text="{Binding Name}"
Style="{StaticResource LabelStyle}"
WidthRequest="200" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
任務項目:
public class TaskItem : INotifyPropertyChanged
{
private MyTask _task;
public MyTask Task
{
get => _task;
set
{
_task = value;
Name = value.Name;
OnPropertyChanged();
}
}
private string _name;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged();
}
}
public string StatusColour => "Green"
}
當我打電話DoTaskGrouping()第二次強制刷新的應用程序與UnhandledException崩潰的問題。我設法跟蹤到一個「具有相同密鑰的項目已被添加」的錯誤,所以我添加了ViewCell.View,認爲它可能會解決問題。請幫忙,我有幾個問題;
- 我該如何解決錯誤「已經添加了具有相同密鑰的項目」併成功刷新了我的分組列表。
- 可觀察集合中的可觀察集合是否自動更新。這意味着如果屬性或它們按變化分組,那麼分組也是如此?意味着物品可以在組之間移動?
- 我的方法是從MVVM的角度來聽的嗎? TaskItem可以做清理。
見鏈接可能是它的幫助下,你 https://forums.xamarin.com/discussion/34229/an-item-with-the-same-key-has-already-been-added-exception- when-adding-nested-scrollview-to-xaml – Pratik
謝謝,我讀過並且以前添加了ViewCell.View。可悲的是這並沒有幫助我的情況,仍然在刷新崩潰。 – wonea
_userTasksGrouped創建在哪裏? – hugoterelle