-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
SwipeView throw exception on windows #8870
Comments
The native exception from WinUI:
|
I'm seeing a similar issue in my simple MAUI application with an ObservableCollection inside a CollectionView. I can add items, but once I remove an item and then try to add another item I get the UnhandledException: |
[UPDATE]
The binding source "Items" is an ObservableCollection{string} inside the ViewModel. |
Are there any recommended workarounds for this? There's no conditional compilation in XAML, so the only other thing I can think of is having a separate page / view for Windows vs other platforms. |
Thats what I have had to do for now |
The issue is possible to reproduce using a Delete Button
|
UPDATE 2: I still have the issue with VS 2022 17.4.5. UPDATE: I get the same exception now on Windows. It worked with VS 2022 17.3.2. Reproduce:
Try out:
|
Hi @hujun-open. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. 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. |
Is this fixed? Can we close? |
@mattleibow I'm curious, how @hujun-open would be expected to know if it is fixed, is the merged PR available somewhere? I submitted issue #9423 which was closed as a duplicate of this issue, as well as #9233 which is probably fixed by the same PR, so I'd like a fix too but I'm not sure how to go about discovering if this is it until the PR changes are released. Did you have some mechanism in mind? |
I ran into this issue about two days ago, right after upgrading to .NET 7 release. So it's definitely unsolved. |
I'm using .net8 preview 5 and the problem still exists for me, my project is using windows and mac desktop, and iOS and Android. Working fine in all but Windows where it crashes because I have a swipe view inside a collection view. Is this being worked on for .net8 ? |
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. |
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! |
Could be related with: microsoft/microsoft-ui-xaml#2527 Can reproduce the issue even with the changes:
|
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 IssueI was encountering this issue when I deleted an item in my ViewModel's 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 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 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 Now, on to the solution that worked for me. The Good StuffI 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 <?xml version="1.0" encoding="utf-8" ?>
<Frame xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CornellPad"
xmlns:viewmodel="clr-namespace:CornellPad.ViewModels"
xmlns:entry="clr-namespace:CornellPad.Models"
x:DataType="entry:TopicModel"
x:Class="CornellPad.Views.Cards.TopicCard"
CornerRadius="20"
Margin="10,10, 15, 0"
BorderColor="{AppThemeBinding Light={StaticResource Blue200Accent}, Dark={StaticResource Gray400}}">
<Frame.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding GoToTopicViewCommand, Source={RelativeSource AncestorType={x:Type viewmodel:LibraryViewModel}}}"
CommandParameter="{Binding .}"/>
</Frame.GestureRecognizers>
<Grid ColumnDefinitions="120, Auto"
RowDefinitions="130"
Margin="5">
<Image Grid.Column="0"
Source="{Binding Icon}"
HeightRequest="100"
WidthRequest="100"/>
<VerticalStackLayout Grid.Column="1"
Padding="5">
<Border
Padding="10, 6"
Stroke="{AppThemeBinding Light={StaticResource Black}, Dark={StaticResource Gray500}}"
StrokeThickness="3"
StrokeShape="RoundRectangle 0,10,10,0"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Start"
BackgroundColor="{AppThemeBinding Light={StaticResource Blue300Accent}, Dark={StaticResource Gray900}}">
<Label
Text="{Binding TopicName}"
Padding="5"
FontSize="{OnPlatform Default=Subtitle, Android=Small}"/>
</Border>
<Label
Text="{Binding NumberOfNotes, StringFormat='{0} Notes in Topic'}"
Padding="10"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Start"/>
</VerticalStackLayout>
</Grid>
</Frame> 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 LibraryView (my main view) XAML <?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:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
xmlns:local="clr-namespace:CornellPad"
xmlns:viewmodel="clr-namespace:CornellPad.ViewModels"
xmlns:entry="clr-namespace:CornellPad.Models"
xmlns:cards="clr-namespace:CornellPad.Views.Cards"
x:DataType="viewmodel:LibraryViewModel"
x:Class="CornellPad.Views.LibraryView"
x:Name="LibraryViewPage"
Title="{Binding Title}">
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate
x:Key="WithDesktop"
x:DataType="entry:TopicModel">
<cards:TopicCard>
<FlyoutBase.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Text="Delete"
CommandParameter="{Binding .}"
Command="{Binding Source={x:Reference LibraryViewPage}, Path=BindingContext.DeleteTopicCommand}"/>
</MenuFlyout>
</FlyoutBase.ContextFlyout>
</cards:TopicCard>
</DataTemplate>
<DataTemplate
x:Key="WithoutDesktop"
x:DataType="entry:TopicModel">
<SwipeView
IsVisible="{OnPlatform WinUI=False, MacCatalyst=True, Android=True, iOS=True}">
<SwipeView.RightItems>
<SwipeItems>
<SwipeItemView
CommandParameter="{Binding .}"
Command="{Binding DeleteTopicCommand, Source={RelativeSource AncestorType={x:Type viewmodel:LibraryViewModel}}}">
<Frame
BackgroundColor="IndianRed"
WidthRequest="100"
HeightRequest="100"
VerticalOptions="Center"
CornerRadius="50"
Padding="0">
<Label
Text="Delete"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</Frame>
</SwipeItemView>
</SwipeItems>
</SwipeView.RightItems>
<cards:TopicCard/>
</SwipeView>
</DataTemplate>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.ToolbarItems>
<ToolbarItem
Text="Add"
IconImageSource="{AppThemeBinding Light=add_light.png, Dark=add_dark.png}"
Command="{Binding GoToCreateTopicViewCommand}"/>
</ContentPage.ToolbarItems>
<CollectionView
ItemsSource="{Binding TopicEntries}"
ItemTemplate="{OnPlatform WinUI={StaticResource WithDesktop}, MacCatalyst={StaticResource WithDesktop}, Default={StaticResource WithoutDesktop}}"
SelectionMode="None">
</CollectionView>
</ContentPage>
By defining my As an aside, my 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). <ControlTemplate x:Key="ItemTemplate">
<Grid ColumnDefinitions="8,84,*,*"
RowDefinitions="auto,auto"
ColumnSpacing="8"
BackgroundColor="White">
<Image Grid.Column="1"
Grid.Row="0"
Grid.RowSpan="2"
VerticalOptions="Center"
HorizontalOptions="Center"
HeightRequest="36">
<Image.Source>
<FontImageSource Glyph="{x:Static icons:Icon.Tag}"
Color="Blue"
Size="36"
FontFamily="{x:Static icons:Font.IconSet}" />
</Image.Source>
</Image>
<VerticalStackLayout Grid.Column="2"
Grid.Row="0"
Margin="0,8"
Grid.RowSpan="2">
<Label FontSize="14"
Text="{Binding Name}"
LineBreakMode="TailTruncation"
VerticalOptions="Center" />
<Label FontSize="14"
Text="{Binding Info}"
LineBreakMode="TailTruncation"
Margin="0,5,0,5"
TextColor="Gray"
VerticalOptions="Center" />
</VerticalStackLayout>
<Label Grid.Column="3"
Grid.Row="0"
Grid.RowSpan="2"
Margin="0,0,12,0"
VerticalOptions="Center"
HorizontalOptions="End"
FontSize="16"
Text="{Binding Price, Converter={StaticResource CurrencyConverter}}"
TextColor="Purple" />
<BoxView Grid.Column="0"
Grid.ColumnSpan="4"
Grid.Row="1"
HeightRequest="1"
VerticalOptions="End"
Color="LightGray" />
</Grid>
</ControlTemplate>
<DataTemplate x:Key="MobileItemTemplate"
x:DataType="data:ItemModel">
<SwipeView>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem Text="Delete"
IsDestructive="True"
BackgroundColor="Red"
Command="{Binding RemoveCommand}"
CommandParameter="{Binding .}"
IsVisible="{Binding RemoveCommand, Converter={StaticResource NotNullConverter}}" />
</SwipeItems>
</SwipeView.RightItems>
<ContentView ControlTemplate="{StaticResource ItemTemplate}" />
</SwipeView>
</DataTemplate>
<DataTemplate x:Key="DesktopItemTemplate"
x:DataType="data:ItemModel">
<ContentView ControlTemplate="{StaticResource ItemTemplate}">
<FlyoutBase.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Remove"
Command="{Binding RemoveCommand}"
CommandParameter="{Binding .}"
IsEnabled="{Binding RemoveCommand, Converter={StaticResource NotNullConverter}}">
<MenuFlyoutItem.IconImageSource>
<FontImageSource Glyph="{x:Static icons:Icon.Remove}"
FontFamily="{x:Static icons:Font.IconSet}" />
</MenuFlyoutItem.IconImageSource>
</MenuFlyoutItem>
</MenuFlyout>
</FlyoutBase.ContextFlyout>
</ContentView>
</DataTemplate> |
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 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. |
Description
it seems following combintions triggers program to throw an exception of "Value does not fall within the expected range.":
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:
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
Depends on
The text was updated successfully, but these errors were encountered: