当我切换到ListView中的另一个源时,它不会更新

时间:2018-04-26 14:00:27

标签: c# xamarin

我是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);
    }
}

0 个答案:

没有答案