首页 > 解决方案 > 当我切换到 ListView 中的另一个源时,它不会更新

问题描述

我是 Xamarin 的新手,我试图做一个切换列表。因此,当按下按钮时,它会切换到另一个列表。我有 CustomLists 类,它包装了所有这些列表并公开了 ChosenList 属性,它可以访问当前显示的列表。当列表中的条目被删除时,命令属性被调用

public ICommand DeleteTest
{
    get { return new Command<TaskRecord>((s) => OnDelete(s)); }
}
void OnDelete(TaskRecord task)
{
    List.Remove(task);
    IsUnfinishedChanged_ = !task.IsFinished;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ChosenList"));
    System.Diagnostics.Debug.Print("Deleting..");
}

当我删除第一个列表中的条目(程序开始时显示的条目)时,它工作正常。但是一秒钟后,ListView 由于某种原因没有更新

这是我的 XAML 代码

 <ListView ItemsSource="{Binding ChosenList}" IsPullToRefreshEnabled="True" 
          Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="2" SeparatorColor="DimGray">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <ViewCell.ContextActions>
                    <MenuItem Text="Delete" IsDestructive="true"  
                               Command="{Binding Source={x:Reference MainGrid}, 
                        Path=BindingContext.DeleteTest}" 
                              CommandParameter="{Binding .}"/>
                </ViewCell.ContextActions>
                <StackLayout Padding="15,0" VerticalOptions="Center">
                    <Label Text="{Binding Path=Name}" FontSize="Large" Font="Arial"
                           FontAttributes="Bold" VerticalOptions="Center"/>
                    <Label Text="{Binding Path=ShortDescr}" FontSize="Micro" Font="Arial"
                           VerticalOptions="Center"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

编辑:ChosenList 是ListView绑定到的包装器

public List<TaskRecord> ChosenList
{
    get
    {
        return (IsAll_ ? List : Unfinished);
    }
}

编辑2:把整个代码放在这里

private List<TaskRecord> List { get; set; }
private List<TaskRecord> UnfinishedCache_;
private List<TaskRecord> Unfinished
{
    get
    {
        if (IsUnfinishedChanged_)
        {
            UnfinishedCache_ = new List<TaskRecord>();
            foreach (TaskRecord task in List)
            {
                if (!task.IsFinished) UnfinishedCache_.Add(task);
            }
            IsUnfinishedChanged_ = false;
        }
        return UnfinishedCache_;
    }
    set { UnfinishedCache_ = value; }
}

private bool IsUnfinishedChanged_=true;
private bool IsAll_;

public ICommand ListChangeCommand
{
    get { return new Command<string>((s)=>OnListSwitch(s)); }
}
void OnListSwitch(string senderText)
{
    if (senderText == "All" && !IsAll_)
    {
        IsAll_ = true;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ChosenList"));
    }
    else if (senderText == "Unfinished" && IsAll_)
    {
        IsAll_ = false;
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ChosenList"));
    }
}

public ICommand DeleteTest
{
    get { return new Command<TaskRecord>((s) => OnDelete(s)); }
}
void OnDelete(TaskRecord task)
{
    List.Remove(task);
    IsUnfinishedChanged_ = !task.IsFinished;
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ChosenList"));
    System.Diagnostics.Debug.Print("Deleting..");
}

public event PropertyChangedEventHandler PropertyChanged;
public List<TaskRecord> ChosenList
{
    get
    {
        return (IsAll_ ? List : Unfinished);
    }
}

标签: c#xamarin

解决方案


推荐阅读