Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Windows] Fix "PlatformView cannot be null here" Exception using VisualStates #27877

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
Loading
from

Conversation

jsuarezruiz
Copy link
Contributor

Description of Change

Fix "PlatformView cannot be null here" Exception using VisualStates on Windows.

System.InvalidOperationException: PlatformView cannot be null here
   at Microsoft.Maui.Handlers.ViewHandler`2.get_PlatformView()
   at Microsoft.Maui.Handlers.ButtonHandler.MapBackground(IButtonHandler handler, IButton button)
   at Microsoft.Maui.Controls.VisualElement.MapBackgroundColor(IViewHandler handler, IView view)
   at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged)
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
   at Microsoft.Maui.Controls.Setter.Apply(BindableObject target, SetterSpecificity specificity)
   at Microsoft.Maui.Controls.VisualStateManager.GoToState(VisualElement visualElement, String name)
   at Microsoft.Maui.Controls.VisualElement.ChangeVisualState()
   at Microsoft.Maui.Controls.View.ChangeVisualState()
   at Microsoft.Maui.Controls.Button.ChangeVisualState()
   at Microsoft.Maui.Controls.VisualElement.OnIsFocusedPropertyChanged(BindableObject bindable, Object oldvalue, Object newvalue)
   at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged)
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent)
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity)
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindablePropertyKey propertyKey, Object value, SetterSpecificity specificity)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.set_IsFocused(Boolean value)
   at Microsoft.Maui.Handlers.ViewHandler.UpdateIsFocused(Boolean isFocused)
   at Microsoft.Maui.Handlers.ViewHandler.DisconnectingHandler(FrameworkElement platformView)
   at Microsoft.Maui.Handlers.ViewHandler.OnDisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Object platformView)
   at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler()
   at Microsoft.Maui.ViewExtensions.DisconnectHandlers(IView view)
   at Microsoft.Maui.Controls.Page.SendNavigatedFrom(NavigatedFromEventArgs args, Boolean disconnectHandlers)
   at Microsoft.Maui.Controls.NavigationPage.SendNavigated(Page previousPage, NavigationType navigationType)
   at Microsoft.Maui.Controls.NavigationPage.MauiNavigationImpl.<>c__DisplayClass8_0.<OnPopToRootAsync>b__2()

In the added sample, we have two pages, each with a Button to navigate forward and backward. Each Button is styled using VisualStates. After navigating back, ChangeVisualState is invoked which will invoke ButtonHandler.MapBackground. However, the button is null at this point, the handler has been disconnected.
The PR include changes to check if the PlatformView is not null and loaded before invoke the ChangeVisualState method.

Issues Fixed

Fixes #27101

@jsuarezruiz jsuarezruiz added t/bug Something isn't working area-xaml XAML, CSS, Triggers, Behaviors platform/windows labels Feb 18, 2025
@Copilot Copilot AI review requested due to automatic review settings February 18, 2025 12:32
@jsuarezruiz jsuarezruiz requested a review from a team as a code owner February 18, 2025 12:32
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 6 changed files in this pull request and generated no comments.

Files not reviewed (2)
  • src/Controls/tests/TestCases.HostApp/Issues/Issue27101.xaml: Language not supported
  • src/Controls/tests/TestCases.HostApp/Issues/Issue27101SecondPage.xaml: Language not supported
Comments suppressed due to low confidence (1)

src/Controls/tests/TestCases.HostApp/Issues/Issue27101SecondPage.xaml.cs:10

  • The OnNavigateBackButtonClicked method is not covered by tests. Add a test case to ensure this method works as expected.
async void OnNavigateBackButtonClicked(object sender, EventArgs e)

@jsuarezruiz
Copy link
Contributor Author

/rebase

Copy link
Member

@jfversluis jfversluis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this one has some relevant test failures?

@michelmoorlag
Copy link

I am kind of waiting for this fix. Would be nice if we can get this pr merged so it will be in the next version?

@jsuarezruiz
Copy link
Contributor Author

/rebase

IsFocused ? VisualStateManager.CommonStates.Focused : VisualStateManager.CommonStates.Unfocused);
}
}
catch(ObjectDisposedException)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
catch(ObjectDisposedException)
catch (ObjectDisposedException)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about this approach. The PR description mentions:

In the added sample, we have two pages, each with a Button to navigate forward and backward. Each Button is styled using VisualStates. After navigating back, ChangeVisualState is invoked which will invoke ButtonHandler.MapBackground. However, the button is null at this point, the handler has been disconnected.

So why is ChangeVisualState called when the handler was disconnected?

using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: can be file-scoped namespace

@MartyIX
Copy link
Contributor

MartyIX commented May 30, 2025

@jsuarezruiz

The PR include changes to check if the PlatformView is not null and loaded before invoke the ChangeVisualState method.

This does not appear to be true. This PR adds a try-catch or not?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-xaml XAML, CSS, Triggers, Behaviors platform/windows t/bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"PlatformView cannot be null here" Exception in Switch control [Windows]
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.