-
Notifications
You must be signed in to change notification settings - Fork 2k
[Windows] Fix "PlatformView cannot be null here" exception during handler disconnect #35314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a88aef9
Fix #27101: short-circuit ElementHandler.UpdateValue while disconnecting
Copilot 5dfc963
Address review feedback: simplify to PlatformView null guard
Copilot 65adeec
Address review feedback: state-based disconnect guard + Invoke coverage
Copilot b43479b
Simplify fix: catch InvalidOperationException in ChangeVisualState
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/Controls/tests/TestCases.HostApp/Issues/Issue27101.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <local:TestContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:Maui.Controls.Sample.Issues" | ||
| x:Class="Maui.Controls.Sample.Issues.Issue27101MainPage"> | ||
| <local:TestContentPage.Resources> | ||
| <ResourceDictionary> | ||
| <Style x:Key="Issue27101ButtonStyle" TargetType="Button"> | ||
| <Setter Property="TextColor" Value="White" /> | ||
| <Setter Property="BackgroundColor" Value="Purple" /> | ||
| <Setter Property="FontFamily" Value="OpenSansRegular"/> | ||
| <Setter Property="FontSize" Value="14"/> | ||
| <Setter Property="BorderWidth" Value="0"/> | ||
| <Setter Property="CornerRadius" Value="8"/> | ||
| <Setter Property="Padding" Value="14,10"/> | ||
| <Setter Property="MinimumHeightRequest" Value="44"/> | ||
| <Setter Property="MinimumWidthRequest" Value="44"/> | ||
| <Setter Property="VisualStateManager.VisualStateGroups"> | ||
| <VisualStateGroupList> | ||
| <VisualStateGroup x:Name="CommonStates"> | ||
| <VisualState x:Name="Normal" /> | ||
| <VisualState Name="Focused" /> | ||
| <VisualState x:Name="Disabled"> | ||
| <VisualState.Setters> | ||
| <Setter Property="TextColor" Value="{AppThemeBinding Light=Black, Dark=LightGray}" /> | ||
| <Setter Property="BackgroundColor" Value="{AppThemeBinding Light=LightGray, Dark=Gray}" /> | ||
| </VisualState.Setters> | ||
| </VisualState> | ||
| <VisualState Name="PointerOver"> | ||
| <VisualState.Setters> | ||
| <Setter Property="BackgroundColor" Value="Pink" /> | ||
| </VisualState.Setters> | ||
| </VisualState> | ||
| <VisualState Name="Pressed"> | ||
| <VisualState.Setters> | ||
| <Setter Property="BackgroundColor" Value="Red" /> | ||
| </VisualState.Setters> | ||
| </VisualState> | ||
| </VisualStateGroup> | ||
| </VisualStateGroupList> | ||
| </Setter> | ||
| </Style> | ||
| </ResourceDictionary> | ||
| </local:TestContentPage.Resources> | ||
| <local:TestContentPage.Content> | ||
| <VerticalStackLayout | ||
| Padding="30,0" | ||
| Spacing="25"> | ||
| <Button | ||
| x:Name="NavigateButton" | ||
| AutomationId="NavigateButton" | ||
| Text="Navigate to Second Page" | ||
| Clicked="OnNavigateButtonClicked" | ||
| HorizontalOptions="Fill" | ||
| Style="{StaticResource Issue27101ButtonStyle}"/> | ||
| </VerticalStackLayout> | ||
| </local:TestContentPage.Content> | ||
| </local:TestContentPage> |
27 changes: 27 additions & 0 deletions
27
src/Controls/tests/TestCases.HostApp/Issues/Issue27101.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| namespace Maui.Controls.Sample.Issues | ||
| { | ||
| [Issue(IssueTracker.Github, 27101, "PlatformView cannot be null here Exception in Windows")] | ||
| public class Issue27101 : NavigationPage | ||
| { | ||
| public Issue27101() : base(new Issue27101MainPage()) | ||
| { | ||
| } | ||
| } | ||
|
|
||
| public partial class Issue27101MainPage : TestContentPage | ||
| { | ||
| public Issue27101MainPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
|
|
||
| protected override void Init() | ||
| { | ||
| } | ||
|
|
||
| void OnNavigateButtonClicked(object sender, EventArgs e) | ||
| { | ||
| Navigation.PushAsync(new Issue27101SecondPage()); | ||
| } | ||
| } | ||
| } | ||
58 changes: 58 additions & 0 deletions
58
src/Controls/tests/TestCases.HostApp/Issues/Issue27101SecondPage.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:Maui.Controls.Sample.Issues" | ||
| x:Class="Maui.Controls.Sample.Issues.Issue27101SecondPage"> | ||
| <ContentPage.Resources> | ||
| <ResourceDictionary> | ||
| <Style x:Key="Issue27101ButtonStyle" TargetType="Button"> | ||
| <Setter Property="TextColor" Value="White" /> | ||
| <Setter Property="BackgroundColor" Value="Purple" /> | ||
| <Setter Property="FontFamily" Value="OpenSansRegular"/> | ||
| <Setter Property="FontSize" Value="14"/> | ||
| <Setter Property="BorderWidth" Value="0"/> | ||
| <Setter Property="CornerRadius" Value="8"/> | ||
| <Setter Property="Padding" Value="14,10"/> | ||
| <Setter Property="MinimumHeightRequest" Value="44"/> | ||
| <Setter Property="MinimumWidthRequest" Value="44"/> | ||
| <Setter Property="VisualStateManager.VisualStateGroups"> | ||
| <VisualStateGroupList> | ||
| <VisualStateGroup x:Name="CommonStates"> | ||
| <VisualState x:Name="Normal" /> | ||
| <VisualState Name="Focused" /> | ||
| <VisualState x:Name="Disabled"> | ||
| <VisualState.Setters> | ||
| <Setter Property="TextColor" Value="{AppThemeBinding Light=Black, Dark=LightGray}" /> | ||
| <Setter Property="BackgroundColor" Value="{AppThemeBinding Light=LightGray, Dark=Gray}" /> | ||
| </VisualState.Setters> | ||
| </VisualState> | ||
| <VisualState Name="PointerOver"> | ||
| <VisualState.Setters> | ||
| <Setter Property="BackgroundColor" Value="Pink" /> | ||
| </VisualState.Setters> | ||
| </VisualState> | ||
| <VisualState Name="Pressed"> | ||
| <VisualState.Setters> | ||
| <Setter Property="BackgroundColor" Value="Red" /> | ||
| </VisualState.Setters> | ||
| </VisualState> | ||
| </VisualStateGroup> | ||
| </VisualStateGroupList> | ||
| </Setter> | ||
| </Style> | ||
| </ResourceDictionary> | ||
| </ContentPage.Resources> | ||
| <ContentPage.Content> | ||
| <VerticalStackLayout | ||
| Padding="30,0" | ||
| Spacing="25"> | ||
| <Button | ||
| x:Name="NavigateBackButton" | ||
| AutomationId="NavigateBackButton" | ||
| Text="Navigate Back" | ||
| Clicked="OnNavigateBackButtonClicked" | ||
| HorizontalOptions="Fill" | ||
| Style="{StaticResource Issue27101ButtonStyle}"/> | ||
| </VerticalStackLayout> | ||
| </ContentPage.Content> | ||
| </ContentPage> |
15 changes: 15 additions & 0 deletions
15
src/Controls/tests/TestCases.HostApp/Issues/Issue27101SecondPage.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace Maui.Controls.Sample.Issues | ||
| { | ||
| public partial class Issue27101SecondPage : ContentPage | ||
| { | ||
| public Issue27101SecondPage() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
|
|
||
| async void OnNavigateBackButtonClicked(object sender, EventArgs e) | ||
| { | ||
| await Navigation.PopAsync(); | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue27101.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue27101 : _IssuesUITest | ||
| { | ||
| public Issue27101(TestDevice device) | ||
| : base(device) | ||
| { } | ||
|
|
||
| public override string Issue => "PlatformView cannot be null here Exception in Windows"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.Button)] | ||
| public void NoCrashWhenNavigatingBackFromPageWithFocusedButton() | ||
| { | ||
| // The crash reproduces only when a Button with VisualStates currently holds focus | ||
| // at the moment the page is navigated away from. Repeat the navigation a few times | ||
| // to give the focus race a chance to surface. | ||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| App.WaitForElement("NavigateButton"); | ||
| App.Tap("NavigateButton"); | ||
|
|
||
| App.WaitForElement("NavigateBackButton"); | ||
| App.Tap("NavigateBackButton"); | ||
| } | ||
|
|
||
| // If we got here without an unhandled InvalidOperationException, the regression is fixed. | ||
| App.WaitForElement("NavigateButton"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Core/tests/UnitTests/TestClasses/DisconnectingTrackingHandlerStub.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using System; | ||
| using Microsoft.Maui.Handlers; | ||
|
|
||
| namespace Microsoft.Maui.UnitTests | ||
| { | ||
| // Helper used by the regression test for https://github.com/dotnet/maui/issues/27101. | ||
| // During DisconnectHandler we trigger an UpdateValue call to mimic the cascade caused on | ||
| // Windows by UpdateIsFocused(false) -> ChangeVisualState -> VSM Setters -> mapper. We then | ||
| // assert the mapper was NOT invoked while the handler was tearing down. | ||
| public class DisconnectingTrackingHandlerStub : ViewHandler<Maui.Controls.Button, object> | ||
| { | ||
| readonly Action _onDisconnect; | ||
|
|
||
| public int MapBackgroundCallCount { get; set; } | ||
|
|
||
| public DisconnectingTrackingHandlerStub(Action onDisconnect) | ||
| : base(BuildMapper()) | ||
| { | ||
| _onDisconnect = onDisconnect; | ||
| } | ||
|
|
||
| static IPropertyMapper<IView, IViewHandler> BuildMapper() | ||
| { | ||
| var mapper = new PropertyMapper<IView, IViewHandler>(); | ||
| mapper.Add(nameof(IView.Background), (handler, view) => | ||
| { | ||
| if (handler is DisconnectingTrackingHandlerStub stub) | ||
| { | ||
| stub.MapBackgroundCallCount++; | ||
| } | ||
| }); | ||
| return mapper; | ||
| } | ||
|
|
||
| protected override object CreatePlatformView() => new object(); | ||
|
|
||
| protected override void DisconnectHandler(object platformView) | ||
| { | ||
| base.DisconnectHandler(platformView); | ||
| _onDisconnect?.Invoke(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — added
PlatformAffected.UWPto the[Issue]attribute in 5dfc963. (Used the positional form to match the existing pattern inIssue7156.xaml.cs.)