Fix active canvas null issue.
This commit is contained in:
@@ -51,6 +51,8 @@ public partial class WinoCalendarFlipView : CustomCalendarFlipView, IDisposable
|
|||||||
|
|
||||||
private INotifyCollectionChanged? _trackedItemsSource;
|
private INotifyCollectionChanged? _trackedItemsSource;
|
||||||
private readonly long _itemsSourceCallbackToken;
|
private readonly long _itemsSourceCallbackToken;
|
||||||
|
private FrameworkElement? _pendingActiveElementContainer;
|
||||||
|
private bool _isActiveElementResolutionPending;
|
||||||
|
|
||||||
public WinoCalendarFlipView()
|
public WinoCalendarFlipView()
|
||||||
{
|
{
|
||||||
@@ -119,6 +121,8 @@ public partial class WinoCalendarFlipView : CustomCalendarFlipView, IDisposable
|
|||||||
|
|
||||||
if (SelectedIndex < 0)
|
if (SelectedIndex < 0)
|
||||||
{
|
{
|
||||||
|
CancelPendingActiveElementResolution();
|
||||||
|
|
||||||
if (itemsSource == null || itemsSource.Count == 0)
|
if (itemsSource == null || itemsSource.Count == 0)
|
||||||
{
|
{
|
||||||
ActiveCanvas = null;
|
ActiveCanvas = null;
|
||||||
@@ -128,15 +132,93 @@ public partial class WinoCalendarFlipView : CustomCalendarFlipView, IDisposable
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get container from index - respects virtualization
|
if (TryResolveActiveElements())
|
||||||
if (ContainerFromIndex(SelectedIndex) is FlipViewItem container)
|
|
||||||
{
|
{
|
||||||
ActiveCanvas = container.FindDescendant<WinoDayTimelineCanvas>();
|
CancelPendingActiveElementResolution();
|
||||||
ActiveVerticalScrollViewer = container.FindDescendant<ScrollViewer>();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Container not ready yet - keep the current active elements until OnContainerPrepared updates them.
|
ScheduleActiveElementResolution();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool TryResolveActiveElements()
|
||||||
|
{
|
||||||
|
if (SelectedIndex < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ContainerFromIndex(SelectedIndex) is not FlipViewItem container)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var activeCanvas = container.FindDescendant<WinoDayTimelineCanvas>();
|
||||||
|
var activeVerticalScrollViewer = container.FindDescendant<ScrollViewer>();
|
||||||
|
|
||||||
|
if (activeCanvas == null && activeVerticalScrollViewer == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ActiveCanvas = activeCanvas;
|
||||||
|
ActiveVerticalScrollViewer = activeVerticalScrollViewer;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ScheduleActiveElementResolution()
|
||||||
|
{
|
||||||
|
if (ContainerFromIndex(SelectedIndex) is FrameworkElement container &&
|
||||||
|
!ReferenceEquals(_pendingActiveElementContainer, container))
|
||||||
|
{
|
||||||
|
if (_pendingActiveElementContainer != null)
|
||||||
|
{
|
||||||
|
_pendingActiveElementContainer.Loaded -= PendingActiveElementContainerLoaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
_pendingActiveElementContainer = container;
|
||||||
|
_pendingActiveElementContainer.Loaded += PendingActiveElementContainerLoaded;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_isActiveElementResolutionPending)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_isActiveElementResolutionPending = true;
|
||||||
|
LayoutUpdated += FlipViewLayoutUpdated;
|
||||||
|
|
||||||
|
DispatcherQueue.TryEnqueue(RetryActiveElementResolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RetryActiveElementResolution()
|
||||||
|
{
|
||||||
|
if (TryResolveActiveElements())
|
||||||
|
{
|
||||||
|
CancelPendingActiveElementResolution();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PendingActiveElementContainerLoaded(object sender, RoutedEventArgs e)
|
||||||
|
=> RetryActiveElementResolution();
|
||||||
|
|
||||||
|
private void FlipViewLayoutUpdated(object? sender, object e)
|
||||||
|
=> RetryActiveElementResolution();
|
||||||
|
|
||||||
|
private void CancelPendingActiveElementResolution()
|
||||||
|
{
|
||||||
|
if (_pendingActiveElementContainer != null)
|
||||||
|
{
|
||||||
|
_pendingActiveElementContainer.Loaded -= PendingActiveElementContainerLoaded;
|
||||||
|
_pendingActiveElementContainer = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_isActiveElementResolutionPending)
|
||||||
|
{
|
||||||
|
LayoutUpdated -= FlipViewLayoutUpdated;
|
||||||
|
_isActiveElementResolutionPending = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,6 +297,8 @@ public partial class WinoCalendarFlipView : CustomCalendarFlipView, IDisposable
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
CancelPendingActiveElementResolution();
|
||||||
|
|
||||||
if (_trackedItemsSource != null)
|
if (_trackedItemsSource != null)
|
||||||
{
|
{
|
||||||
_trackedItemsSource.CollectionChanged -= ItemsSourceUpdated;
|
_trackedItemsSource.CollectionChanged -= ItemsSourceUpdated;
|
||||||
|
|||||||
Reference in New Issue
Block a user