maui: SwipeView throw exception on windows
Description
it seems following combintions triggers program to throw an exception of “Value does not fall within the expected range.”:
- CollectioView with SwipeView
- the binding list of collectionview is initiazed with some items
- then clear the list, and add a new item
I have created a simple example to reproduce: https://github.com/hujun-open/mauiswieviewissue
I only found this issue on windows, android works fine.
following are the visual studio version info:
Microsoft Visual Studio Community 2022
Version 17.3.0 Preview 4.0
VisualStudio.17.Preview/17.3.0-pre.4.0+32714.290
Microsoft .NET Framework
Version 4.8.04084
Installed Version: Community
ASP.NET and Web Tools 17.3.372.35679
ASP.NET and Web Tools
Azure App Service Tools v3.0.0 17.3.372.35679
Azure App Service Tools v3.0.0
C# Tools 4.3.0-3.22329.30+29e657c0582904529bae2a87c227220e03f509cf
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.
Extensibility Message Bus 1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.
Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines
Mono Debugging for Visual Studio 17.3.20 (3f4cb00)
Support for debugging Mono processes with Visual Studio.
NuGet Package Manager 6.3.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/
Razor (ASP.NET Core) 17.0.0.2232702+e1d654e792aa2fe6646a6935bcca80ff0aff4387
Provides languages services for ASP.NET Core Razor.
TypeScript Tools 17.0.10701.2001
TypeScript Tools for Microsoft Visual Studio
Visual Basic Tools 4.3.0-3.22329.30+29e657c0582904529bae2a87c227220e03f509cf
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Visual F# Tools 17.1.0-beta.22327.2+ddc90b20287a765a9d526da42b3be0dd8e907ec5
Microsoft Visual F# Tools
Visual Studio IntelliCode 2.2
AI-assisted development for Visual Studio.
VisualStudio.DeviceLog 1.0
Information about my package
VisualStudio.Mac 1.0
Mac Extension for Visual Studio
VSPackage Extension 1.0
VSPackage Visual Studio Extension Detailed Info
Xamarin 17.3.0.288 (main@5d42bb2)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.
Xamarin Designer 17.3.0.204 (remotes/origin/d17-3@f5da0100a)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.
Xamarin.Android SDK 13.0.0.0 (d17-3/030cd63)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: dffa5ab
Java.Interop: xamarin/java.interop/d17-3@7716ae53
SQLite: xamarin/sqlite/3.38.5@df4deab
Xamarin.Android Tools: xamarin/xamarin-android-tools/main@14076a6
Steps to Reproduce
see readme @ https://github.com/hujun-open/mauiswieviewissue
Version with bug
6.0.400
Last version that worked well
Unknown/Other
Affected platforms
Windows
Affected platform versions
windows 10 Enterprise 21H2, OS build 19044.1826
Did you find any workaround?
no
Relevant log output
+ sender {SwipeIssueDemo.WinUI.App} object {SwipeIssueDemo.WinUI.App}
- e {Microsoft.UI.Xaml.UnhandledExceptionEventArgs} Microsoft.UI.Xaml.UnhandledExceptionEventArgs
- Exception {"Value does not fall within the expected range."} System.Exception {System.ArgumentException}
+ Data {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
HResult -2147024809 int
HelpLink null string
+ InnerException null System.Exception
Message "Value does not fall within the expected range." string
ParamName null string
Source null string
StackTrace null string
TargetSite null System.Reflection.MethodBase
+ Static members
+ Non-Public members
Handled false bool
Message "The parameter is incorrect.\r\n" string
+ Non-Public members
Depends on
VS bug #1870771
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 5
- Comments: 31 (5 by maintainers)
Commits related to this issue
- NR: Remove SwipeView as it is responsible for a crash on the Windows platform. See https://github.com/dotnet/maui/issues/8870 — committed to jbe2277/waf by jbe2277 4 years ago
- NR: Remove SwipeView as it creates a crash on Windows. https://github.com/dotnet/maui/issues/8870 — committed to jbe2277/waf by jbe2277 3 years ago
- NR: Use separate ItemTemplate for Windows because using a SwipeView crashes on Windows. https://github.com/dotnet/maui/issues/8870 — committed to jbe2277/waf by jbe2277 3 years ago
I don’t know if this will help the majority of projects encountering this problem, but I have found an All-XAML workaround to this problem. This should work with any control that uses a DataTemplate, but, as I am fairly new to .NET MAUI / Xamarin, I don’t know how far this workaround will extend to everyone’s issues. I do hope that this helps someone, though.
I have broken this long post into two parts, the background for my issue (in case some of you are facing something similar, or where this information may be useful to someone). And, a second section called ‘The Good Stuff’. If you just want to see the solution that I used, skip the first section (it may be a waste of your time to read it anyway). 😃
Background for My Issue
I was encountering this issue when I deleted an item in my ViewModel’s
ObservableCollection(which is bound to aCollectionViewin my main view’s XAML) and then added a new item during the same session. On Windows it would crash the application, but it worked fine on Android (I didn’t test it on iOS or MacOS). This crash occurred during theShell.Current.GoToAsync()method, which I am using to pass an object back to the main view’s VM, using theApplyQueryAttributes()method to capture the passed in data. All of the application’s data is being stored to a SQLite database, which was important to me in figuring out that it wasn’t just anullvalue of some sort causing a crash.Execution would follow as one would expect, by entering the command I have defined to create the data object (when the ‘Create’ button is pressed) and call
Shell.Current.GoToAsync()to go back to the main view, passing the data object. The data object is valid; my immediate thought was that somehow it may benullwhen I encountered the error, but it contained all the necessary data. The execution then jumped fromGoToAsync()to the VM’sApplyQueryAttributes()method where it correctly processed the data object by adding it to myObservableCollectionand storing it in the database. Good, no issues. This is where things seem strange to me, because then execution jumped back to theGoToAsync()method again, even though I haveawait-ed theGoToAsync()method. And this point in the execution is where the application would crash. To be clear, the data object is added to theObservableCollectionand to the database successfully. After the crash, I can start the app up again and the added object (which caused the crash) will be in theCollectionView…so, it was added to the database successfully, and adding the object to theObservbleCollectionis not where the app crashed. It was only when execution jumped back toGoToAsync()that things went sideways.Again, I am new to .NET MAUI and to be completely honest, I am fairly new to C# (although I have been programming in C/C++ for many years). I was under the (clearly misguided) belief that the application would wait for the
GoToAsync()method’s execution to complete before jumping to theApplyQueryAttributes()method in my ViewModel since theApplyQueryAttributes()method needs the information being passed throughGoToAsync()and that the main thread should have to halt for that information to be passed. To me, this seems strange that it would begin executing theGoToAsync()method, jump to theApplyQueryAttributes()method, and then jump back to theGoToAsync()method. Meanwhile, all of the data being passed through theGoToAsync()method is valid, it is received correctly in my VM and is fully processed, and then the execution ofGoToAsync()completes. 🤷I am only including all of this information for completeness. I don’t know how much of this actually applies to the underlying issue with the
SwipeViewcontrol, of if it matters at all.Now, on to the solution that worked for me.
The Good Stuff
I am an inherently lazy person, and since I intent to make all the source code to my app open source, I am just going to copy-paste what I have to fix this problem.
TopicCard View XAML
There is nothing special about this. Literally the only changed that I had to make in the code-behind was changing the parent class to
Frameinstead ofContentView. I just copied the XAML from my main view and pasted it in here, with a few alterations to wire everything up, of course.LibraryView (my main view) XAML
By defining my
DataTemplates as a resource with a key, I can then easily access them according to the platform I am compiling for. I didn’t have to change any code-behind anywhere (with the one small exception mentioned above), nor did I have to change any of the code in my ViewModel. By breaking out the XAML markup into it’s own separate ‘card’, I still have one source to define the UI/UX elements for that card. I was very concerned about having duplicate XAML markup, but this seems to circumvent that issue. [Looks around nervously]As an aside, my
xmlns:entryshould be namedxmns:models, which I plan to change. But as I said, I am inherently lazy and I haven’t gotten around to that yet. So, you get the XAML markup with warts and all. 😛Despite the issues that I have encountered, I have to say that I ❤️ .NET MAUI, even if we have to live with some issues. I hope this helps someone and that you all have a great day.
Thank you @Rabidgoalie for the awesome work-around! I didn’t want to turn my template into a view, so I used a ControlTemplate instead and it works great! I was tired of having to maintain 2 templates (1 for swipe and 1 for desktop).
This is a native crash on WinUI3 and has nothing tot much to do with MAUI. I am working on a plain UWP app without MAUI and it’s crashing for me as well.
It’s reproducible once you have SwipeControl items in a ListView data template and scroll quickly up and down. In UWP it is fired as stowed exception. My investigation with WinDbg showed that this line of code throws it
https://github.com/microsoft/microsoft-ui-xaml/blob/66a7b0ae71c19f89c6a7d86a1986794ad1a1bf09/src/controls/dev/SwipeControl/SwipeControl.cpp#L786
My guess is that once you share LeftItems and RightItems as a resource, interaction tracker code loses child-parent relation with the owner due to UI virtualization.
I just ran into the same problem with Visual Studio 17.4.5 and MAUI 7.0.59 in a demo project I’m developing for a conference where I’ll be talking about .NET MAUI and wanted to share my workaround.
I was able to get around the issue by migrating my XAML code to C# markup using the helper methods from the [.NET MAUI Community Toolkit] (https://learn.microsoft.com/de-de/dotnet/communitytoolkit/maui/markup/markup). Within the C# markup file, I was able to use conditional compilation to exclude the SwipeView on Windows.
See:
https://github.com/AndreKraemer/basta-2023-se-maui-demo/blob/fb33963810b7788795fcf6445d4d5f4cf256e26b/ConferenceMauiDemo/Views/SessionsPageMarkup.cs#L152
Since the
SwipeViewdoesn’t work on Windows when using the application with a mouse, it wasn’t a big deal to exclude it from my view. To give Windows users the same functionality that users of other platforms have with theSwipeView, I added a context menu (right click). See: https://github.com/AndreKraemer/basta-2023-se-maui-demo/blob/fb33963810b7788795fcf6445d4d5f4cf256e26b/ConferenceMauiDemo/Views/SessionsPageMarkup.cs#L238Since this was used to close at least #9540, #9423 and #8870 there seems to be a fair amount of interest. PR #11229 seems like it corrects this but says “The PR require some changes in the public API. Will maintain the changes up to date but need to wait to .NET 8.”
Fair enough, but if breaking changes are the problem at least put something in the community toolkit temporarily because waiting for .NET 8 to fix a bug in .NET 6 seems excessive.
I agree, I have a swipe view for use on mobile devices and am using context menus for desktop. At a minimum we need the presence of a swipe view in a collection view to at least not crash!
Absent a response from Microsoft I can only guess, but it looks like it’s being worked on. I noticed a while back that PR #11229 by @jsuarezruiz seems like it corrects this but says “The PR require some changes in the public API. Will maintain the changes up to date but need to wait to .NET 8.” That was in November of 2022, however it looks like a new decision has been reached and PR #11229 was closed at the beginning of July 2023 with “We are going to close this PR focusing on fix crashes or any problem that prevents the use of SwipeView in Windows while we continue to promote the implementations and accessibility on the desktop (keyboard support etc).”
A SwipeView without mouse support is not a very practical desktop UI element but as long as its presence does not crash the app (as it currently does in this example) it shouldn’t be too hard to code around that and offer an alternative desktop interface. Not hard, but tedious.
This issue still exists with Visual Studio 17.4.5 and MAUI 7.0.59.
How to reproduce:
MainPage.xamlwithMainPage.xaml.cswithInsert itemsbutton until the items do not fit in the view anymore and would need a scrollbar -> Exception occurs. See screen cast:Note: Remove the
SwipeViewfrom theDataTemplate. Then this error does not occur.[UPDATE] I just discovered the issue was with the SwipeView inside the CollectionView. I hope someone on the MAUI team is working on the bugs with the SwipeView control. I have taken it out for now; not sure how a developer would make this work on a phone?.. for now I’m just going to show a delete button.
The binding source “Items” is an ObservableCollection{string} inside the ViewModel. You can uncomment the SwipeView to see the exception happen.
I’m seeing a similar issue in my simple MAUI application with an ObservableCollection<string> inside a CollectionView. I can add items, but once I remove an item and then try to add another item I get the UnhandledException:
[Microsoft.UI.Xaml.UnhandledExceptionEventArgs] Exception = {"Value does not fall within the expected range."} Message = {"The parameter is incorrect."}I also have defensive code in each add/remove method to make sure the collection is not null and the item has content before removing or adding.The native exception from WinUI: