Skip to content

Commit

Permalink
25421- ListViewRenderer Crash in HostApp and BindingError was occured…
Browse files Browse the repository at this point in the history
… in windows platforms (#25453)

* 25421- ListViewRenderer Crash in HostApp

* Review concerns.

* changes removed
  • Loading branch information
vishnumenon2684 authored Oct 24, 2024
1 parent f124490 commit 9939516
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -319,47 +319,51 @@ protected override void Dispose(bool disposing)

if (disposing)
{
if (List != null)
{
foreach (ViewToHandlerConverter.WrapperControl wrapperControl in FindDescendants<ViewToHandlerConverter.WrapperControl>(List))
{
wrapperControl.CleanUp();
}

if (_subscribedToTapped)
{
_subscribedToTapped = false;
List.Tapped -= ListOnTapped;
}
if (_subscribedToItemClick)
{
_subscribedToItemClick = false;
List.ItemClick -= OnListItemClicked;
}

List.SelectionChanged -= OnControlSelectionChanged;
if (_collectionViewSource != null)
_collectionViewSource.Source = null;

List.DataContext = null;
CleanUpResources();
}

// Leaving this here as a warning because setting this to null causes
// an AccessViolationException if you run Issue1975
// List.ItemsSource = null;
base.Dispose(disposing);
}

List = null;
void CleanUpResources()
{
if (List != null)
{
foreach (ViewToHandlerConverter.WrapperControl wrapperControl in FindDescendants<ViewToHandlerConverter.WrapperControl>(List))
{
wrapperControl.CleanUp();
}

if (_zoom != null)
if (_subscribedToTapped)
{
_zoom.ViewChangeCompleted -= OnViewChangeCompleted;
_zoom = null;
_subscribedToTapped = false;
List.Tapped -= ListOnTapped;
}
if (_subscribedToItemClick)
{
_subscribedToItemClick = false;
List.ItemClick -= OnListItemClicked;
}

List.SelectionChanged -= OnControlSelectionChanged;
if (_collectionViewSource != null)
_collectionViewSource.Source = null;

List.DataContext = null;

// Leaving this here as a warning because setting this to null causes
// an AccessViolationException if you run Issue1975
// List.ItemsSource = null;

List = null;
}

base.Dispose(disposing);
if (_zoom != null)
{
_zoom.ViewChangeCompleted -= OnViewChangeCompleted;
_zoom = null;
}
}

static IEnumerable<T> FindDescendants<T>(DependencyObject dobj) where T : DependencyObject
{
int count = VisualTreeHelper.GetChildrenCount(dobj);
Expand Down Expand Up @@ -800,6 +804,12 @@ void OnListItemClicked(object sender, ItemClickEventArgs e)
}
}

private protected override void DisconnectHandlerCore()
{
CleanUpResources();
base.DisconnectHandlerCore();
}

void OnControlSelectionChanged(object sender, WSelectionChangedEventArgs e)
{
bool areEqual = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,65 +126,77 @@ protected override void SetBackground(Brush brush)
}
}

private protected override void DisconnectHandlerCore()
{
CleanUpResources();
base.DisconnectHandlerCore();
}

protected override void Dispose(bool disposing)
{
if (_disposed)
return;

if (disposing)
{
if (Element != null)
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
templatedItems.GroupedCollectionChanged -= OnGroupedCollectionChanged;
}
CleanUpResources();
}

if (_dataSource != null)
{
_dataSource.Dispose();
_dataSource = null;
}
_disposed = true;

if (_tableViewController != null)
{
_tableViewController.Dispose();
_tableViewController = null;
}
base.Dispose(disposing);
}

if (_headerRenderer != null)
{
_headerRenderer.VirtualView?.DisposeModalAndChildHandlers();
_headerRenderer = null;
}
if (_footerRenderer != null)
{
_footerRenderer.VirtualView?.DisposeModalAndChildHandlers();
_footerRenderer = null;
}
bool _disposed = false;

if (_backgroundUIView != null)
{
_backgroundUIView.Dispose();
_backgroundUIView = null;
}
void CleanUpResources()
{
if (Element != null)
{
var templatedItems = TemplatedItemsView.TemplatedItems;
templatedItems.CollectionChanged -= OnCollectionChanged;
templatedItems.GroupedCollectionChanged -= OnGroupedCollectionChanged;
}

var headerView = ListView?.HeaderElement as VisualElement;
if (headerView != null)
headerView.MeasureInvalidated -= OnHeaderMeasureInvalidated;
Control?.TableHeaderView?.Dispose();
if (_dataSource != null)
{
_dataSource.Dispose();
_dataSource = null;
}

var footerView = ListView?.FooterElement as VisualElement;
if (footerView != null)
footerView.MeasureInvalidated -= OnFooterMeasureInvalidated;
Control?.TableFooterView?.Dispose();
if (_tableViewController != null)
{
_tableViewController.Dispose();
_tableViewController = null;
}

_disposed = true;
if (_headerRenderer != null)
{
_headerRenderer.VirtualView?.DisposeModalAndChildHandlers();
_headerRenderer = null;
}
if (_footerRenderer != null)
{
_footerRenderer.VirtualView?.DisposeModalAndChildHandlers();
_footerRenderer = null;
}

base.Dispose(disposing);
if (_backgroundUIView != null)
{
_backgroundUIView.Dispose();
_backgroundUIView = null;
}

var headerView = ListView?.HeaderElement as VisualElement;
if (headerView != null)
headerView.MeasureInvalidated -= OnHeaderMeasureInvalidated;
Control?.TableHeaderView?.Dispose();

var footerView = ListView?.FooterElement as VisualElement;
if (footerView != null)
footerView.MeasureInvalidated -= OnFooterMeasureInvalidated;
Control?.TableFooterView?.Dispose();
}
bool _disposed = false;
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
_requestedScroll = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public CorePageView(Page rootPage)
template.SetBinding(TextCell.TextProperty, "Title");
template.SetBinding(TextCell.AutomationIdProperty, "TitleAutomationId");

BindingContext = _pages;
ItemTemplate = template;
ItemsSource = _pages;

Expand Down

0 comments on commit 9939516

Please sign in to comment.