Share via

collectview set selected single , but it go in selected mulity when added <ContentPage.Resources> and <SwipeView>

Fei Xu 490 Reputation points
2023-04-06T06:54:56.7433333+00:00

CollectionView SelectionMode="Single"  
but it is selected multiple under blow xml 
no <ContentPage.Resources> it is single , no <SwipeView> it is single too , both them it go wrong ?

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="myMolly.Views.MyScoresPage"
             Title="收藏" Shell.NavBarIsVisible="False">
    <ContentPage.Resources>
        <Style TargetType="SwipeView">
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal" />
                        <VisualState x:Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor"
                                        Value="LightSkyBlue" />
                               
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    <VerticalStackLayout Margin="5">
        <CollectionView x:Name="cvScores"  ItemsSource="{Binding Scores}"  SelectionMode="Single">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems >
                                <SwipeItem Text="删除"
                                           IconImageSource="appicon.png"
                                           BackgroundColor="LightPink"
                                           Command="{Binding Source={x:Reference cvScores}, Path=BindingContext.DeleteCommand}"
                                           CommandParameter="{Binding}" />
                            </SwipeItems>
                        </SwipeView.RightItems>
                        <Grid Padding="5">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                            <Border Grid.RowSpan="2"  StrokeShape="RoundRectangle 10,10,10,10" >
                               <Image 
                               Source="{Binding ImageUrl}" 
                               Aspect="AspectFill"
                               HeightRequest="60" 
                               WidthRequest="60" />
                            </Border>
                            <Label Grid.Column="1" 
                               Text="{Binding Title}" 
                               FontAttributes="Bold" />
                        <Label Grid.Row="1"
                               Grid.Column="1" 
                               Text="{Binding Creator}"
                               FontAttributes="None" 
                               VerticalOptions="End" />
                        </Grid>
                    </SwipeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </VerticalStackLayout>
</ContentPage>
Developer technologies | .NET | .NET MAUI
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Anonymous
    2023-04-06T07:55:01.69+00:00

    Hello Fei Xu,

    =================Update====================

    System.AccessViolationException when using SwipeView in a CollectionView on Windows System.AccessViolationException when using SwipeView in a CollectionView on Windows This is a known issue. System.AccessViolationException when using SwipeView in a CollectionView on Windows This is a known issue. See MAUI github links: System.AccessViolationException when using SwipeView in a CollectionView on Windows This is a known issue. See MAUI github links: System.AccessViolationException when using SwipeView in a CollectionView on Windows - back again #9540

    SwipeView throw exception on windows

    And this known issue can not be solved now. Please don't use SwipeView in a CollectionView before this issue is solved.

    Then I test with ListView, when you are running in windows platform with following code, this error do not show it and you can scroll it successfully without add heightrequest like Collectionview, but SwipeView do not show in window platform.

    <ContentPage.Resources>
            <Style TargetType="StackLayout">
                <Setter Property="VisualStateManager.VisualStateGroups">
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="Selected">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor"
                                            Value="LightSkyBlue" />
                               </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </Setter>
            </Style>
        </ContentPage.Resources>
        <StackLayout>
           <Button Text="scool"></Button>
    
           <ListView x:Name="cvScores" Margin="5" ItemsSource="{Binding Scores}"   HasUnevenRows="True"  >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout>
                                <SwipeView>
                                    <SwipeView.RightItems>
                                        <SwipeItems >
                                            <SwipeItem Text="删除"
                                                   IconImageSource="appicon.png"
                                                   BackgroundColor="LightPink"
                                                   Command="{Binding Source={x:Reference cvScores}, Path=BindingContext.DeleteCommand}"
                                                   CommandParameter="{Binding}" />
                                        </SwipeItems>
                                    </SwipeView.RightItems>
                                    <Grid Padding="5">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto" />
                                            <RowDefinition Height="Auto" />
                                        </Grid.RowDefinitions>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto" />
                                            <ColumnDefinition Width="Auto" />
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.RowSpan="2"  StrokeShape="RoundRectangle 10,10,10,10" >
                                            <Image
                                       Source="{Binding ImageUrl}"
                                       Aspect="AspectFill"
                                       HeightRequest="60"
                                       WidthRequest="60" />
                                        </Border>
                                        <Label Grid.Column="1"
                                       Text="{Binding Title}"
                                       FontAttributes="Bold" />
                                        <Label Grid.Row="1"
                                       Grid.Column="1"
                                       Text="{Binding Creator}"
                                       FontAttributes="None"
                                       VerticalOptions="End" >
                                        </Label>
                                    </Grid>
                                </SwipeView>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
       </StackLayout>
    </ContentPage>
    

    Best Regards, Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.