.net c#限制observablecollection中的条目数

我有一个WPF应用程序,其中UI具有一个列表框。列表框具有ObservableCollection的绑定。日志类实现INotifyPropertyChanged。


该列表将显示应用程序的连续日志记录。只要该应用程序正在运行。ObservableCollection的大小不断增长。一段时间后,出现内存不足异常。我想在列表控件中显示最新的1000个条目。关于此的任何建议将有很大帮助!!


XAML:


                    <DataGrid AutoGenerateColumns="False" SelectedValue="{Binding SelectedLog}" SelectionUnit="FullRow" SelectionMode="Single" Name="dataGridLogs" 

                      ItemsSource="{Binding Path=LogList}"  CanUserReorderColumns="True" CanUserResizeRows="True" CanUserDeleteRows="False"  IsReadOnly="True"

                      CanUserAddRows="False" EnableColumnVirtualization="True" EnableRowVirtualization="True" SelectionChanged="grid_SelectionChanged"> 

                <DataGrid.Columns>

                    <DataGridTextColumn Header="Time Stamp" Binding="{Binding StrTimeStamp, Mode=OneWay}" Width="Auto"/>

                    <DataGridTextColumn Header="Action" Binding="{Binding Action, Mode=OneWay}" Width="Auto"/>


            </DataGrid>

ViewModel:


    public ObservableCollection<LogData> LogList

    {

        get

        {

            if (logList == null)

            {

                logList = new ObservableCollection<LogData>();

            }

            return logList;

        }

        set

        {

            logList = value;

            OnPropertyChanged("LogList");

        }

    }

模型:


     public class LogData : INotifyPropertyChanged

{

    public LogData()

    {

    }

    private String timestamp = string.Empty;

    public String StrTimestamp

    {

        get

        {

            if (timestamp == null)

                return string.Empty;

            return timestamp ;

        }

        set

        {


            timestamp = value;

        }

    }

    public string Action

    {

       get;set;

    }

}


动漫人物
浏览 591回答 3
3回答

素胚勾勒不出你

您可以创建自己的大小受限的可观察集合类。这样的事情应该可以帮助您入门:public class LimitedSizeObservableCollection<T> : INotifyCollectionChanged{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private ObservableCollection<T> _collection;&nbsp; &nbsp; private bool _ignoreChange;&nbsp; &nbsp; public LimitedSizeObservableCollection(int capacity)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Capacity = capacity;&nbsp; &nbsp; &nbsp; &nbsp; _ignoreChange = false;&nbsp; &nbsp; &nbsp; &nbsp; _collection = new ObservableCollection<T>();&nbsp; &nbsp; &nbsp; &nbsp; _collection.CollectionChanged += _collection_CollectionChanged;&nbsp; &nbsp; }&nbsp; &nbsp; public event NotifyCollectionChangedEventHandler CollectionChanged;&nbsp; &nbsp; public int Capacity {get;}&nbsp; &nbsp; public void Add(T item)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(_collection.Count = Capacity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ignoreChange = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _collection.RemoveAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _ignoreChange = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; _collection.Add(item);&nbsp; &nbsp; }&nbsp; &nbsp; private void _collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(!_ignoreChange)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CollectionChanged?.Invoke(this, e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}当然,您可能必须公开更多方法,但是我希望这足以使您理解。

芜湖不芜

可以通过此类轻松完成:public class LimitedSizeObservableCollection<T> : ObservableCollection<T>{&nbsp; &nbsp; public int Capacity { get; }&nbsp; &nbsp; public LimitedSizeObservableCollection(int capacity)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Capacity = capacity;&nbsp; &nbsp; }&nbsp; &nbsp; public new void Add(T item)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Count >= Capacity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.RemoveAt(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; base.Add(item);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP