Skip to content
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

App crashes with ArgumentOutOfRangeException when FlyoutItemIsVisible changes (WinUI only) #16929

Closed
BretJohnson opened this issue Aug 22, 2023 · 8 comments
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@BretJohnson
Copy link
Member

BretJohnson commented Aug 22, 2023

Description

When FlyoutItemIsVisible changes, the app can crash on WinUI with an ArgumentOutOfRangeException.

This is an issue that @spadapet originally reported here #10468 (comment), now split out to a separate bug.

REPRO:

  • Open solution
  • F5 to build and run on Windows
  • Click the one button that appears in the running app UI

RESULT:
ArgumentOutOfRangeException, in the WinUI runtime code.

Callstack:

System.Private.CoreLib.dll!System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
WinRT.Runtime.dll!WinRT.ExceptionHelpers.ThrowExceptionForHR.__Throw|20_0(int hr)
WinRT.Runtime.dll!ABI.System.Collections.Specialized.NotifyCollectionChangedEventHandler.NativeDelegateWrapper.Invoke(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
System.ObjectModel.dll!System.Collections.ObjectModel.ObservableCollection<object>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Platform.ShellView.UpdateMenuItemSource()
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Handlers.ShellHandler.MapFlyoutItems(Microsoft.Maui.Controls.Handlers.ShellHandler handler, Microsoft.Maui.Controls.Shell view)
Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper<Microsoft.Maui.Controls.Shell, Microsoft.Maui.Controls.Handlers.ShellHandler>.Add.AnonymousMethod__0(Microsoft.Maui.IElementHandler h, Microsoft.Maui.IElement v)
Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdatePropertyCore(string key, Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView)
Microsoft.Maui.dll!Microsoft.Maui.PropertyMapper.UpdateProperty(Microsoft.Maui.IElementHandler viewHandler, Microsoft.Maui.IElement virtualView, string property)
Microsoft.Maui.dll!Microsoft.Maui.Handlers.ElementHandler.UpdateValue(string property)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Shell..ctor.AnonymousMethod__154_2(object _, System.EventArgs __)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellFlyoutItemsManager.CheckIfFlyoutItemsChanged()
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Shell.SendFlyoutItemsChanged()
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellSection.SendStructureChanged()
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellSection..ctor.AnonymousMethod__42_0(object _, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellElementCollection.OnVisibleItemsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs args)
System.ObjectModel.dll!System.Collections.ObjectModel.ObservableCollection<Microsoft.Maui.Controls.ShellContent>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
System.Private.CoreLib.dll!System.Collections.ObjectModel.Collection<Microsoft.Maui.Controls.ShellContent>.Remove(Microsoft.Maui.Controls.ShellContent item)
System.Private.CoreLib.dll!System.Collections.ObjectModel.Collection<Microsoft.Maui.Controls.ShellContent>.System.Collections.IList.Remove(object value)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellElementCollection.CheckVisibility(Microsoft.Maui.Controls.BaseShellItem element)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.ShellElementCollection.BaseShellItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.OnPropertyChanged(string propertyName)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Element.OnPropertyChanged(string propertyName)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BaseShellItem.OnPropertyChanged(string propertyName)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValueActual(Microsoft.Maui.Controls.BindableProperty property, Microsoft.Maui.Controls.BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, Microsoft.Maui.Controls.Internals.SetValueFlags attributes, Microsoft.Maui.Controls.SetterSpecificity specificity, bool silent)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindableObject.SetValueCore(Microsoft.Maui.Controls.BindableProperty property, object value, Microsoft.Maui.Controls.Internals.SetValueFlags attributes, Microsoft.Maui.Controls.BindableObject.SetValuePrivateFlags privateAttributes, Microsoft.Maui.Controls.SetterSpecificity specificity)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindingExpression.ApplyCore(object sourceObject, Microsoft.Maui.Controls.BindableObject target, Microsoft.Maui.Controls.BindableProperty property, bool fromTarget, Microsoft.Maui.Controls.SetterSpecificity specificity)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindingExpression.Apply(bool fromTarget)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged.AnonymousMethod__50_0()
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(Microsoft.Maui.Dispatching.IDispatcher dispatcher, System.Action action)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs args)
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
>	MauiApp4.dll!MauiApp4.ViewModel.ShowStuff.set(bool value) Line 19
MauiApp4.dll!MauiApp4.MainPage.OnCounterClicked(object sender, System.EventArgs e) Line 14
Microsoft.Maui.Controls.dll!Microsoft.Maui.Controls.Button.Microsoft.Maui.Controls.Internals.IButtonElement.PropagateUpClicked()

Version with bug

8.0.0-preview.7.8842

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

No response

Did you find any workaround?

No. I tried several different options for work arounds and none were completely successful.

Relevant log output

No response

@BretJohnson BretJohnson added the t/bug Something isn't working label Aug 22, 2023
@BretJohnson BretJohnson added this to the Backlog milestone Aug 22, 2023
@ghost
Copy link

ghost commented Aug 22, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@Quaybe
Copy link

Quaybe commented Aug 22, 2023

I get this too. .NET 7, latest VS updates, both preview and non-preview. I do permissions updates regularly in my app, and if we remove permissions, I remove a flyout item to that module. On WinUI, it crashes when this happens. Works as expected on Android, haven't had a chance to test iOS, but I'm getting there.

@BretJohnson
Copy link
Member Author

This is a WinUI bug, also reproducible in a native WinUI app. I'll write a separate bug for the WinUI team shortly with more details & link to it here. @Quaybe - iOS should be fine, but of course please let us know if you find otherwise.

@BretJohnson BretJohnson changed the title App crashes when FlyoutItemIsVisible changes, on WinUI App crashes with ArgumentOutOfRangeException when FlyoutItemIsVisible changes, on WinUI Aug 22, 2023
@BretJohnson BretJohnson changed the title App crashes with ArgumentOutOfRangeException when FlyoutItemIsVisible changes, on WinUI App crashes with ArgumentOutOfRangeException when FlyoutItemIsVisible changes (WinUI only) Aug 22, 2023
BretJohnson added a commit that referenced this issue Aug 22, 2023
MenuShellItem wraps a MenuItem. Shell related properties should be
synchronized between the two objects, always the same values.
Add code so that FlyoutItemIsVisible is also synchronized. Most properties are
synchroned via bindings, but FlyoutItemIsVisible is an attached property so
we need to have code that runs on property changes instead.

Fixes issue #16929

Note that normally the MenuItem is specified in XAML while MenuShellItem is accessed
in code behind, thus the need to synchronize when FlyoutItemIsVisible is initialized in XAML
then updated later via code behind, like in the bug.
@BretJohnson
Copy link
Member Author

BretJohnson commented Aug 23, 2023

The real bug here is in WinUI, tracked with the WinUI bug here: microsoft/microsoft-ui-xaml#8770
I tried various workarounds in MAUI code to avoid the WinUI crash, but none of them are good options for us. WinUI should really fix.

PureWeen pushed a commit that referenced this issue Aug 24, 2023
MenuShellItem wraps a MenuItem. Shell related properties should be
synchronized between the two objects, always the same values.
Add code so that FlyoutItemIsVisible is also synchronized. Most properties are
synchroned via bindings, but FlyoutItemIsVisible is an attached property so
we need to have code that runs on property changes instead.

Fixes issue #16929

Note that normally the MenuItem is specified in XAML while MenuShellItem is accessed
in code behind, thus the need to synchronize when FlyoutItemIsVisible is initialized in XAML
then updated later via code behind, like in the bug.
@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Aug 30, 2023
@XamlTest
Copy link

Verified this on Visual Studio Enterprise 17.8.0 Preview 1.0. Repro on Windows 11 .NET 8 with below Project:
TestAppThatHitsArgumentOutOfRange.zip

@Eilon Eilon added area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/windows 🪟 labels Nov 9, 2023
@busitech
Copy link

@PureWeen PureWeen added the s/try-latest-version Please try to reproduce the potential issue on the latest public version label May 3, 2024
Copy link
Contributor

Hi @BretJohnson. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@PureWeen
Copy link
Member

PureWeen commented May 3, 2024

I had to fix the routes so that we don't have duplicate routes but it appears that this is fixed now with the latest winui update

 <ShellContent
     Title="Home 1"
     ContentTemplate="{DataTemplate local:MainPage}"
     Route="MainPage1"
     FlyoutItem.IsVisible="{Binding ShowStuff}" />

 <ShellContent
     Title="Home 2"
     ContentTemplate="{DataTemplate local:MainPage}"
     Route="MainPage2"
     FlyoutItem.IsVisible="{Binding ShowStuff}" />

 <ShellContent
     Title="Home 3"
     ContentTemplate="{DataTemplate local:MainPage}"
     Route="MainPage3"
     FlyoutItem.IsVisible="{Binding UnshowStuff}" />

@PureWeen PureWeen closed this as completed May 3, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/windows 🪟 s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants