CurrencyManager Collection - 处理 SuspendBinding 和 ResumeBinding






3.33/5 (3投票s)
管理应用程序中所有货币管理器的 SuspendBindings 和 ResumeBindings。
引言
由于我的应用程序处理大量记录,数据绑定的挂起功能对于性能至关重要。我经常花费大量时间来确保我在应用程序的所有区域都挂起数据绑定。 拥有十几个或更多具有绑定上下文的活动屏幕是很常见的。
挂起绑定的一个常见原因是为了能够在验证发生之前更改多个字段。这些情况可以根据业务逻辑单独处理。
在填充数据集时,绑定控件上可以触发大量事件,无论是在网格中还是在单个控件上。 我发现,在加载或清除数据时挂起绑定可以显著提高性能。
带有事件的集合 (CollectionWithEvents)
这是一个从 CollectionBase
继承并为集合提供事件的类。 CurrencyManagerCollection
类从 CollectionWithEvents
继承。
using System;
using System.Collections;
namespace Assemblies.UserInterfaceArchitecture
{
// summary
// Base class for managing strongly typed collections
// summary
public class CollectionWithEvents : CollectionBase
{
// Declare the event signatures
public delegate void CollectionClear();
public delegate void CollectionChange(int index, object value);
// Collection change events
public event CollectionClear Clearing;
public event CollectionClear Cleared;
public event CollectionChange Inserting;
public event CollectionChange Inserted;
public event CollectionChange Removing;
public event CollectionChange Removed;
// Overrides for generating events
protected override void OnClear()
{
if (Clearing != null) Clearing();
}
protected override void OnClearComplete()
{
if (Cleared != null) Cleared();
}
protected override void OnInsert(int index, object value)
{
if (Inserting != null) Inserting(index, value);
}
protected override void OnInsertComplete(int index, object value)
{
if (Inserted != null) Inserted(index, value);
}
protected override void OnRemove(int index, object value)
{
if (Removing != null) Removing(index, value);
}
protected override void OnRemoveComplete(int index, object value)
{
if (Removed != null) Removed(index, value);
}
}
}
货币管理器集合 (CurrencyManagerCollection)
using System;
using System.Data ;
using System.Windows.Forms ;
Assemblies.UserInterfaceArchitecture
{
/// <SUMMARY>
/// Utility for handling a collection of currencymanagers.
/// If we add all of the currency managers to this collection,
/// we can easily 'SuspendBinding' or 'ResumeBinding' on all
///currency managers at the same time,
///when we're changing the underlying lists,
///through searching or clearing, or whatever
/// </SUMMARY>
public class CurrencyManagerCollection : CollectionWithEvents
{
/// <SUMMARY>
/// suspend bindings for all currency managers in collection
/// </SUMMARY>
public void SuspendBindings()
{
foreach (CurrencyManager cm in this)
{
cm.SuspendBinding() ;
}
}
/// <SUMMARY>
/// resume bindings for all currency managers in collection
/// </SUMMARY>
public void ResumeBindings()
{
foreach (CurrencyManager cm in this)
{
cm.ResumeBinding() ;
}
}
public int Add(CurrencyManager value)
{
//make sure we're not trying to add the same object
if (Contains(value) == false)
{
return base.List.Add(value as object);
}
return 0 ;
}
public void Remove(CurrencyManager value)
{
base.List.Remove(value as object);
}
public void Insert(int index, CurrencyManager value)
{
base.List.Insert(index, value as object);
}
public bool Contains(CurrencyManager value)
{
return base.List.Contains(value as object);
}
public CurrencyManager this[int index]
{
get { return (base.List[index] as CurrencyManager); }
}
}
}
用法
当在设置了数据绑定的控件/窗体中触发事件时,您只需要将 CurrencyManager
添加到集合中即可。 以下是一些示例:
// Suppose we have a 'mainClass' that
// has a CurrencyManagerCollection class called CurrencyManagers
// this will add the currency manager for the current form/control
// for the binding context of the 'TheDataView'
mainClass.CurrencyManagers.Add(((CurrencyManager)
this.BindingContext[TheDataView]));
// this will add the currency manager for the current form/control
// for the binding context of the 'TheDataTable'
mainClass.CurrencyManagers.Add(((CurrencyManager)
this.BindingContext[TheDataTable]));
// if you already have a CurrencyManager set up, you can just add it.
// if your CurrencyManager was called 'currencyManger' then:
mainClass.CurrencyManagers.Add(currencyManager);
在准备将数据加载到 DataSet
中时(希望它是一个强类型的 DataSet
!),您可以执行以下操作:
mainClass.CurrencyManagers.SuspendBindings() ;
//your code here that populates the DataSet
mainClass.CurrencyManagers.ResumeBindings() ;
在某些情况下,我不想挂起应用程序中的所有绑定,但如果需要,我知道我所有的 databound 控件都会被挂起!