Skip to content

Commit 9939516

Browse files
25421- ListViewRenderer Crash in HostApp and BindingError was occured in windows platforms (#25453)
* 25421- ListViewRenderer Crash in HostApp * Review concerns. * changes removed
1 parent f124490 commit 9939516

File tree

3 files changed

+96
-75
lines changed

3 files changed

+96
-75
lines changed

src/Controls/src/Core/Compatibility/Handlers/ListView/Windows/ListViewRenderer.cs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -319,47 +319,51 @@ protected override void Dispose(bool disposing)
319319

320320
if (disposing)
321321
{
322-
if (List != null)
323-
{
324-
foreach (ViewToHandlerConverter.WrapperControl wrapperControl in FindDescendants<ViewToHandlerConverter.WrapperControl>(List))
325-
{
326-
wrapperControl.CleanUp();
327-
}
328-
329-
if (_subscribedToTapped)
330-
{
331-
_subscribedToTapped = false;
332-
List.Tapped -= ListOnTapped;
333-
}
334-
if (_subscribedToItemClick)
335-
{
336-
_subscribedToItemClick = false;
337-
List.ItemClick -= OnListItemClicked;
338-
}
339-
340-
List.SelectionChanged -= OnControlSelectionChanged;
341-
if (_collectionViewSource != null)
342-
_collectionViewSource.Source = null;
343-
344-
List.DataContext = null;
322+
CleanUpResources();
323+
}
345324

346-
// Leaving this here as a warning because setting this to null causes
347-
// an AccessViolationException if you run Issue1975
348-
// List.ItemsSource = null;
325+
base.Dispose(disposing);
326+
}
349327

350-
List = null;
328+
void CleanUpResources()
329+
{
330+
if (List != null)
331+
{
332+
foreach (ViewToHandlerConverter.WrapperControl wrapperControl in FindDescendants<ViewToHandlerConverter.WrapperControl>(List))
333+
{
334+
wrapperControl.CleanUp();
351335
}
352336

353-
if (_zoom != null)
337+
if (_subscribedToTapped)
354338
{
355-
_zoom.ViewChangeCompleted -= OnViewChangeCompleted;
356-
_zoom = null;
339+
_subscribedToTapped = false;
340+
List.Tapped -= ListOnTapped;
357341
}
342+
if (_subscribedToItemClick)
343+
{
344+
_subscribedToItemClick = false;
345+
List.ItemClick -= OnListItemClicked;
346+
}
347+
348+
List.SelectionChanged -= OnControlSelectionChanged;
349+
if (_collectionViewSource != null)
350+
_collectionViewSource.Source = null;
351+
352+
List.DataContext = null;
353+
354+
// Leaving this here as a warning because setting this to null causes
355+
// an AccessViolationException if you run Issue1975
356+
// List.ItemsSource = null;
357+
358+
List = null;
358359
}
359360

360-
base.Dispose(disposing);
361+
if (_zoom != null)
362+
{
363+
_zoom.ViewChangeCompleted -= OnViewChangeCompleted;
364+
_zoom = null;
365+
}
361366
}
362-
363367
static IEnumerable<T> FindDescendants<T>(DependencyObject dobj) where T : DependencyObject
364368
{
365369
int count = VisualTreeHelper.GetChildrenCount(dobj);
@@ -800,6 +804,12 @@ void OnListItemClicked(object sender, ItemClickEventArgs e)
800804
}
801805
}
802806

807+
private protected override void DisconnectHandlerCore()
808+
{
809+
CleanUpResources();
810+
base.DisconnectHandlerCore();
811+
}
812+
803813
void OnControlSelectionChanged(object sender, WSelectionChangedEventArgs e)
804814
{
805815
bool areEqual = false;

src/Controls/src/Core/Compatibility/Handlers/ListView/iOS/ListViewRenderer.cs

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -126,65 +126,77 @@ protected override void SetBackground(Brush brush)
126126
}
127127
}
128128

129+
private protected override void DisconnectHandlerCore()
130+
{
131+
CleanUpResources();
132+
base.DisconnectHandlerCore();
133+
}
134+
129135
protected override void Dispose(bool disposing)
130136
{
131137
if (_disposed)
132138
return;
133139

134140
if (disposing)
135141
{
136-
if (Element != null)
137-
{
138-
var templatedItems = TemplatedItemsView.TemplatedItems;
139-
templatedItems.CollectionChanged -= OnCollectionChanged;
140-
templatedItems.GroupedCollectionChanged -= OnGroupedCollectionChanged;
141-
}
142+
CleanUpResources();
143+
}
142144

143-
if (_dataSource != null)
144-
{
145-
_dataSource.Dispose();
146-
_dataSource = null;
147-
}
145+
_disposed = true;
148146

149-
if (_tableViewController != null)
150-
{
151-
_tableViewController.Dispose();
152-
_tableViewController = null;
153-
}
147+
base.Dispose(disposing);
148+
}
154149

155-
if (_headerRenderer != null)
156-
{
157-
_headerRenderer.VirtualView?.DisposeModalAndChildHandlers();
158-
_headerRenderer = null;
159-
}
160-
if (_footerRenderer != null)
161-
{
162-
_footerRenderer.VirtualView?.DisposeModalAndChildHandlers();
163-
_footerRenderer = null;
164-
}
150+
bool _disposed = false;
165151

166-
if (_backgroundUIView != null)
167-
{
168-
_backgroundUIView.Dispose();
169-
_backgroundUIView = null;
170-
}
152+
void CleanUpResources()
153+
{
154+
if (Element != null)
155+
{
156+
var templatedItems = TemplatedItemsView.TemplatedItems;
157+
templatedItems.CollectionChanged -= OnCollectionChanged;
158+
templatedItems.GroupedCollectionChanged -= OnGroupedCollectionChanged;
159+
}
171160

172-
var headerView = ListView?.HeaderElement as VisualElement;
173-
if (headerView != null)
174-
headerView.MeasureInvalidated -= OnHeaderMeasureInvalidated;
175-
Control?.TableHeaderView?.Dispose();
161+
if (_dataSource != null)
162+
{
163+
_dataSource.Dispose();
164+
_dataSource = null;
165+
}
176166

177-
var footerView = ListView?.FooterElement as VisualElement;
178-
if (footerView != null)
179-
footerView.MeasureInvalidated -= OnFooterMeasureInvalidated;
180-
Control?.TableFooterView?.Dispose();
167+
if (_tableViewController != null)
168+
{
169+
_tableViewController.Dispose();
170+
_tableViewController = null;
181171
}
182172

183-
_disposed = true;
173+
if (_headerRenderer != null)
174+
{
175+
_headerRenderer.VirtualView?.DisposeModalAndChildHandlers();
176+
_headerRenderer = null;
177+
}
178+
if (_footerRenderer != null)
179+
{
180+
_footerRenderer.VirtualView?.DisposeModalAndChildHandlers();
181+
_footerRenderer = null;
182+
}
184183

185-
base.Dispose(disposing);
184+
if (_backgroundUIView != null)
185+
{
186+
_backgroundUIView.Dispose();
187+
_backgroundUIView = null;
188+
}
189+
190+
var headerView = ListView?.HeaderElement as VisualElement;
191+
if (headerView != null)
192+
headerView.MeasureInvalidated -= OnHeaderMeasureInvalidated;
193+
Control?.TableHeaderView?.Dispose();
194+
195+
var footerView = ListView?.FooterElement as VisualElement;
196+
if (footerView != null)
197+
footerView.MeasureInvalidated -= OnFooterMeasureInvalidated;
198+
Control?.TableFooterView?.Dispose();
186199
}
187-
bool _disposed = false;
188200
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
189201
{
190202
_requestedScroll = null;

src/Controls/tests/TestCases.HostApp/CoreViews/CorePageView.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public CorePageView(Page rootPage)
119119
template.SetBinding(TextCell.TextProperty, "Title");
120120
template.SetBinding(TextCell.AutomationIdProperty, "TitleAutomationId");
121121

122-
BindingContext = _pages;
123122
ItemTemplate = template;
124123
ItemsSource = _pages;
125124

0 commit comments

Comments
 (0)