fix(legacy): finalize hosted module lifecycle cleanup

This commit is contained in:
SugarChes
2026-08-29 18:03:44 +08:00
parent e27d445844
commit 176952d52c
5 changed files with 806 additions and 88 deletions
@@ -44,19 +44,39 @@ namespace Lskj.Control.Model
Dictionary<BandedGridView, List<BandedGridDragGrid>> registrations,
BandedGridView gridView)
{
if (registrations == null)
return;
foreach (KeyValuePair<BandedGridView, List<BandedGridDragGrid>> entry in registrations.ToList())
{
bool removeKey = ReferenceEquals(entry.Key, gridView);
foreach (BandedGridDragGrid registration in entry.Value.ToList())
List<BandedGridDragGrid> registrationsForView = entry.Value;
if (registrationsForView == null)
{
if (removeKey || registration == null || registration._disposed || registration.References(gridView))
registrations.Remove(entry.Key);
continue;
}
foreach (BandedGridDragGrid registration in registrationsForView.ToList())
{
if (removeKey || registration == null || registration._disposed ||
registration.References(gridView))
{
if (registration != null)
registration.Dispose();
entry.Value.Remove(registration);
{
try
{
registration.Dispose();
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("清理带状表格拖拽注册失败:" + exception);
}
}
registrationsForView.Remove(registration);
}
}
if (removeKey || entry.Value.Count == 0)
if (removeKey || registrationsForView.Count == 0)
registrations.Remove(entry.Key);
}
}
@@ -65,23 +85,71 @@ namespace Lskj.Control.Model
Dictionary<GridView, List<BandedGridDragGrid>> registrations,
GridView gridView)
{
if (registrations == null)
return;
foreach (KeyValuePair<GridView, List<BandedGridDragGrid>> entry in registrations.ToList())
{
bool removeKey = ReferenceEquals(entry.Key, gridView);
foreach (BandedGridDragGrid registration in entry.Value.ToList())
List<BandedGridDragGrid> registrationsForView = entry.Value;
if (registrationsForView == null)
{
if (removeKey || registration == null || registration._disposed || registration.References(gridView))
registrations.Remove(entry.Key);
continue;
}
foreach (BandedGridDragGrid registration in registrationsForView.ToList())
{
if (removeKey || registration == null || registration._disposed ||
registration.References(gridView))
{
if (registration != null)
registration.Dispose();
entry.Value.Remove(registration);
{
try
{
registration.Dispose();
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("清理带状表格拖拽注册失败:" + exception);
}
}
registrationsForView.Remove(registration);
}
}
if (removeKey || entry.Value.Count == 0)
if (removeKey || registrationsForView.Count == 0)
registrations.Remove(entry.Key);
}
}
private static bool IsDisposedView(BandedGridView view)
{
if (view == null)
return true;
try
{
return view.GridControl == null || view.GridControl.IsDisposed;
}
catch
{
return true;
}
}
private static bool IsDisposedView(GridView view)
{
if (view == null)
return true;
try
{
return view.GridControl == null || view.GridControl.IsDisposed;
}
catch
{
return true;
}
}
internal bool References(GridView gridView)
{
return gridView != null &&
@@ -124,8 +192,14 @@ namespace Lskj.Control.Model
/// <param name="tarGridView">目标GridView.</param>
public BandedGridDragGrid(BandedGridView sourceView, GridView targetView)
{
// Do not retain an incomplete registration in the global dictionaries.
if (IsDisposedView(sourceView) || IsDisposedView(targetView))
return;
this._sourceGridView = sourceView;
this._targetGridView = targetView;
try
{
//记录所有拖拽类到来源表中,后每次进入目标表禁用其他拖拽类的拖拽事件
if (StaticBandedControl.BandedGridViewDragGridDic.ContainsKey(sourceView))
{
@@ -171,6 +245,21 @@ namespace Lskj.Control.Model
{
this._targetGridView.Disposed += OnGridViewDisposed;
}
}
catch
{
// Roll back dictionary entries and event handlers from a partial
// registration before surfacing the original failure.
try
{
Dispose();
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("回滚带状表格拖拽注册失败:" + exception);
}
throw;
}
}
private void OnGridViewDisposed(object sender, EventArgs e)
@@ -225,7 +314,8 @@ namespace Lskj.Control.Model
if (ReferenceEquals(
StaticBandedControl.SourceDragBandedGridView,
sourceGridView))
sourceGridView) &&
!HasRegistrationForView(sourceGridView))
{
StaticBandedControl.SourceDragBandedGridView = null;
}
@@ -237,6 +327,37 @@ namespace Lskj.Control.Model
_targetGridView = null;
}
private static bool HasRegistrationForView(BandedGridView gridView)
{
if (gridView == null)
return false;
List<BandedGridDragGrid> items;
if (StaticBandedControl.BandedGridViewDragGridDic != null &&
StaticBandedControl.BandedGridViewDragGridDic.TryGetValue(gridView, out items) &&
ContainsLiveRegistration(items, gridView))
return true;
if (StaticBandedControl.BandedTargetViewDragGridDic != null)
foreach (KeyValuePair<GridView, List<BandedGridDragGrid>> entry in
StaticBandedControl.BandedTargetViewDragGridDic)
if (ContainsLiveRegistration(entry.Value, gridView))
return true;
return false;
}
private static bool ContainsLiveRegistration(
IEnumerable<BandedGridDragGrid> registrations,
GridView gridView)
{
if (registrations == null || gridView == null)
return false;
foreach (BandedGridDragGrid item in registrations)
if (item != null && !item._disposed && item.References(gridView))
return true;
return false;
}
private static void RemoveRegistration<TKey>(
Dictionary<TKey, List<BandedGridDragGrid>> registrations,
TKey key,
@@ -248,12 +369,23 @@ namespace Lskj.Control.Model
return;
}
if (registrations == null)
{
return;
}
List<BandedGridDragGrid> registrationsForView;
if (!registrations.TryGetValue(key, out registrationsForView))
{
return;
}
if (registrationsForView == null)
{
registrations.Remove(key);
return;
}
registrationsForView.Remove(registration);
if (registrationsForView.Count == 0)
{