From cf30c136f52e05ffd2260ddc4da02a4b66222ba0 Mon Sep 17 00:00:00 2001 From: Tamilarasan Paranthaman <93904422+Tamilarasan-Paranthaman@users.noreply.github.com> Date: Tue, 10 Dec 2024 15:44:12 +0530 Subject: [PATCH] [Windows] Fix for ListView ScrollTo Method throws NullReferenceException (#26385) * ListView ScrollTo issue fix * Optimized the fix. * Revert unwanted changes * ListView ScrollTo issue fix * Optimized the fix. * Revert unwanted changes * Enabling test cases. --- .../Handlers/ListView/Windows/ListViewRenderer.cs | 5 +++-- .../Tests/Issues/Bugzilla/Bugzilla42956.cs | 6 ++---- .../Tests/Issues/XFIssue/Issue3788.cs | 3 +-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Controls/src/Core/Compatibility/Handlers/ListView/Windows/ListViewRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/ListView/Windows/ListViewRenderer.cs index 40c723d078d8..c7ddfc9ccaf9 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/ListView/Windows/ListViewRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/ListView/Windows/ListViewRenderer.cs @@ -19,6 +19,7 @@ using Microsoft.UI.Xaml.Data; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; +using Microsoft.Maui.ApplicationModel; using Windows.Foundation; using Specifics = Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific.ListView; using UwpScrollBarVisibility = Microsoft.UI.Xaml.Controls.ScrollBarVisibility; @@ -597,12 +598,12 @@ async void ScrollTo(object group, object item, ScrollToPosition toPosition, bool if (viewer == null) { RoutedEventHandler loadedHandler = null; - loadedHandler = async (o, e) => + loadedHandler = (o, e) => { List.Loaded -= loadedHandler; // Here we try to avoid an exception, see explanation at bottom - await Control.Dispatcher.RunIdleAsync(args => { ScrollTo(group, item, toPosition, shouldAnimate, includeGroup); }); + MainThread.BeginInvokeOnMainThread(() => { ScrollTo(group, item, toPosition, shouldAnimate, includeGroup); }); }; List.Loaded += loadedHandler; return; diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla42956.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla42956.cs index ec25b01ed58f..c4595c6b7869 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla42956.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla42956.cs @@ -1,5 +1,4 @@ -#if TEST_FAILS_ON_WINDOWS //App was crashing while open on windows, getting Object Null Reference exception on ListViewRenderer, issue: https://github.com/dotnet/maui/issues/26345 -using NUnit.Framework; +using NUnit.Framework; using UITest.Appium; using UITest.Core; @@ -22,5 +21,4 @@ public void Bugzilla42956Test() App.WaitForElement(Success); } } -} -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/XFIssue/Issue3788.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/XFIssue/Issue3788.cs index 6dd8bc73f994..b3f6b3beb38f 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/XFIssue/Issue3788.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/XFIssue/Issue3788.cs @@ -1,5 +1,4 @@ -#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_CATALYST // In Windows, getting object null reference exception while running the sample, Issue: https://github.com/dotnet/maui/issues/26345 -//In MacCatalyst, Timeout Exception in the label line no 20. tried by using App.QueryUntilPresent, and adding delay also won't work. +#if TEST_FAILS_ON_CATALYST //In MacCatalyst, Timeout Exception in the label line no 20. tried by using App.QueryUntilPresent, and adding delay also won't work. using NUnit.Framework; using UITest.Appium; using UITest.Core;