更改ObservableCollection时,FlowListView未完全更新UI <t>

时间:2017-07-22 17:20:54

标签: mvvm xamarin.forms observablecollection

当我从列表中删除对象时,UI不会反映列表的当前状态,通常只会使一个项目出现问题。

示例:如果我删除4个项目,则在UI上仅显示我删除了3个项目

 private ObservableCollection<Card> _cards;

    public ObservableCollection<Card> Cards
    {
        get
        {
            if (_cards == null)
            {
                _cards = new ObservableCollection<Card>();
                return _cards;
            }

            return _cards;
        }
        set
        {
            SetValue(ref _cards, value);
        }
    }

我的删除方法

private void RemoveFromCards(Card card)
    {
        for (int i = Cards.Count - 1; i >= 0; i--)
        {
            if (Cards[i].Id == card.Id)
            {
                Cards.RemoveAt(i);
                /* I tried this but doesn't work also 
                 * 
                 * 
                    ObservableCollection<Card> copy = Cards;
                    copy.RemoveAt(i);
                    Cards = copy;
                */
            }
        }
    }

调用删除方法

 private void RemoveCardsFromView(List<Archive> cards)
    {
        foreach (Archive a in cards)
        {
            Card c = new Card {Id = a.CardId};
            RemoveFromCards(c);
        }
    }

致电代码

 public async Task RefreshCardsView()
    {
        if (!CrossConnectivity.Current.IsConnected)
        {
            BuildToast(false,"No internet connection");
            return;
        }

        try
        {
            JsonResult cards = (JsonResult) await HttpMiddleman.GetCards();
            RemoveCardsFromView(cards.Data.DeletedCards);
            AddCardsToView(cards.Data.AddedCards);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
            BuildToast(false,"Could not refresh");
        }               
    }

我在FlowListView中的绑定

<controls:FlowListView x:Name="Board" HasUnevenRows="True"
                               BackgroundColor="Black"
                               FlowColumnCount="2"
                               FlowItemTappedCommand="{Binding ExecuteActionCommand}"
                               FlowColumnMinWidth="110"
                               IsPullToRefreshEnabled="True"
                               IsRefreshing="{Binding IsRefreshing}"
                               RefreshCommand="{Binding RefreshViewCommand}"
                               FlowItemsSource="{Binding Cards}"> .....

先谢谢你们

1 个答案:

答案 0 :(得分:0)

确保在属性语句中的SetValue()之后正确触发OnPropertyChanged

尝试使用此类(来自xamarin)并传递卡片的可相关性而不是卡片对象。

实施例

private void RemoveFromCards(Card card)
{
    Cards.RemoveRange(new []{card});
}

//or directly from

private void RemoveCardsFromView(List<Archive> cards)
{
    Cards.RemoveRange(cards.Select(s=> new Card {Id = a.CardId}));
} 

/// <summary> 
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed. 
/// </summary> 
/// <typeparam name="T"></typeparam> 
public class ObservableRangeCollection<T> : ObservableCollection<T>
{

    /// <summary> 
    /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class. 
    /// </summary> 
    public ObservableRangeCollection()
        : base()
    {
    }

    /// <summary> 
    /// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection. 
    /// </summary> 
    /// <param name="collection">collection: The collection from which the elements are copied.</param> 
    /// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception> 
    public ObservableRangeCollection(IEnumerable<T> collection)
        : base(collection)
    {
    }

    /// <summary> 
    /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). 
    /// </summary> 
    public void AddRange(IEnumerable<T> collection, NotifyCollectionChangedAction notificationMode = NotifyCollectionChangedAction.Add)
    {
        if (collection == null)
            throw new ArgumentNullException("collection");

        CheckReentrancy();

        if (notificationMode == NotifyCollectionChangedAction.Reset)
        {
            foreach (var i in collection)
            {
                Items.Add(i);
            }

            OnPropertyChanged(new PropertyChangedEventArgs("Count"));
            OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

            return;
        }

        int startIndex = Count;
        var changedItems = collection is List<T> ? (List<T>)collection : new List<T>(collection);
        foreach (var i in changedItems)
        {
            Items.Add(i);
        }

        OnPropertyChanged(new PropertyChangedEventArgs("Count"));
        OnPropertyChanged(new PropertyChangedEventArgs("Item[]"));
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, changedItems, startIndex));
    }

    /// <summary> 
    /// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T). 
    /// </summary> 
    public void RemoveRange(IEnumerable<T> collection)
    {
        if (collection == null)
            throw new ArgumentNullException("collection");

        foreach (var i in collection)
            Items.Remove(i);
        OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    /// <summary> 
    /// Clears the current collection and replaces it with the specified item. 
    /// </summary> 
    public void Replace(T item)
    {
        ReplaceRange(new T[] { item });
    }

    /// <summary> 
    /// Clears the current collection and replaces it with the specified collection. 
    /// </summary> 
    public void ReplaceRange(IEnumerable<T> collection)
    {
        if (collection == null)
            throw new ArgumentNullException("collection");

        Items.Clear();
        AddRange(collection, NotifyCollectionChangedAction.Reset);
    }
}
相关问题