Skip to content

Conversation

@japarson
Copy link
Contributor

@japarson japarson commented Mar 19, 2024

Description of Change

When we call DisplayActionSheet from a modal dialog, we encounter an issue with the MauiContext at the following line:

var current = sender.ToPlatform(VirtualView.RequireMauiContext());

In this situation, VirtualView.RequireMauiContext() will return the MauiContext for the Window. This is problematic as it causes some strange behavior when converting the view to a Handler. Here, we can see that the view's Handler will be different from the Window's Handler`.

if (handler?.MauiContext != null && handler.MauiContext != context)
handler = null;

As a result, we end up getting a new Handler from the Windows MauiContext and setting it here:

view.Handler = handler;

When we set the new Handler, the modal dialog actually ends up disappearing, while the window itself is still in a modal state, rendering it unusable:
14829_freeze

Furthermore, we end up getting a null pageParent which leads us to attempt showing the ActionSheet from the current view:

if (current != null && current is FrameworkElement mainPage)
actionSheet.ShowAt(current);

However, when we execute this here we end up hitting an exception related to a null XamlRoot:
image

To make matters worse, when we catch the exception we attempt to use UI.Xaml.Window.Current to show the ActionSheet which is also null.

if (UI.Xaml.Window.Current != null && UI.Xaml.Window.Current.Content is FrameworkElement mainPage)
actionSheet.ShowAt(mainPage);

We can avoid this whole debacle by using sender.RequireMauiContext() instead.

Demo

14829_fix

Issues Fixed

Fixes #14829

@dotnet-policy-service dotnet-policy-service bot added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Mar 19, 2024
@japarson japarson marked this pull request as ready for review March 19, 2024 22:11
@japarson japarson requested a review from a team as a code owner March 19, 2024 22:11
@japarson japarson requested a review from PureWeen March 27, 2024 18:07
@PureWeen PureWeen merged commit 794f224 into main Mar 27, 2024
@PureWeen PureWeen deleted the dev/japarson/14829 branch March 27, 2024 22:15
@github-actions github-actions bot locked and limited conversation to collaborators Apr 27, 2024
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DisplayActionSheet still not working on Windows

6 participants