Xamarin.forms:ListView 中的 SwipeView 问题

Xamarin.forms: Problem with SwipeView in a ListView

提问人:Marcel Delhaye 提问时间:8/7/2023 最后编辑:Marcel Delhaye 更新时间:8/17/2023 访问量:136

问:

我的 Xamarin.Forms 应用程序包含一个 Listview。目前,用户可以通过选择此项在列表视图中查看此项的详细信息。我想使用滑动功能,而不是选择查看项目的详细信息。为此,我使用了 SwipeItem 函数,如 Xamarin.Forms SwipeView 页上的 Microsoft 所述(链接 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/swipeview)。下面是我的应用的 xaml 代码:

        <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:d="http://xamarin.com/schemas/2014/forms/design"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:fab="clr-namespace:Refractored.FabControl;assembly=Refractored.FabControl"
                 xmlns:local="clr-namespace:ButtonCircle.FormsPlugin.Abstractions;assembly=ButtonCircle.FormsPlugin.Abstractions" 
                 xmlns:local1="clr-namespace:MemoCourses.ViewModels"
                 xmlns:input="clr-namespace:Plugin.InputKit.Shared.Controls;assembly=Plugin.InputKit"
                 xmlns:syncfusion="clr-namespace:Syncfusion.SfNumericTextBox.XForms;assembly=Syncfusion.SfNumericTextBox.XForms"
                 xmlns:helpers="clr-namespace:MemoCourses.Helpers"
                 mc:Ignorable="d"
                 x:Class="MemoCourses.Views.Panier_achatsPage"
                 Title="{helpers:Translate Panier_achatsPage_Titre}">

        <ContentPage.ToolbarItems>
            <!--<ToolbarItem x:Name ="menuEmpty" Text="Vider le panier"  Order="Secondary" Clicked="menuEmpty_Clicked"/>-->
            <ToolbarItem x:Name ="menuEmptyAll" Text="{helpers:Translate Panier_achatsPage_Menu01}"  Order="Secondary" Clicked="menuEmptyAll_Clicked"/>
            <ToolbarItem x:Name ="menuAddFavoris" Text="{helpers:Translate Panier_achatsPage_Menu02}"  Order="Secondary" Clicked="menuAddFavoris_Clicked"/>
            <ToolbarItem x:Name ="menuSendMail" Text="{helpers:Translate Panier_achatsPage_Menu03}"  Order="Secondary" Clicked="menuSendMail_Clicked"/>
            <ToolbarItem x:Name ="menuViewCard" Text="{helpers:Translate Panier_achatsPage_Menu04}"  Order="Secondary" Clicked="menuViewCard_Clicked"/>
            <ToolbarItem x:Name= "menuLockScreen" Text="{helpers:Translate Panier_achatsPage_Menu05}" Order="Secondary" Clicked="menuLockScreen_Clicked"/>
            <ToolbarItem x:Name= "menuUnLockScreen" Text="{helpers:Translate Panier_achatsPage_Menu06}" Order="Secondary" Clicked="menuUnLockScreen_Clicked"/>
            <ToolbarItem x:Name= "menuBlockModif" Text="{helpers:Translate Panier_achatsPage_Menu07}" Order="Secondary" Clicked="menuBlockModif_Clicked"/>
            <ToolbarItem x:Name= "menuUnLockModif" Text="{helpers:Translate Panier_achatsPage_Menu08}" Order="Secondary" Clicked="menuUnLockModif_Clicked"/>
        </ContentPage.ToolbarItems>

        <Page.Resources>
            <local1:ImageConverter x:Key="ImageConverter" />
        </Page.Resources>

        <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

            <Grid AbsoluteLayout.LayoutFlags="All"
                      AbsoluteLayout.LayoutBounds="0,1,1,1">

                <ListView x:Name="PanierListView"
                        HasUnevenRows="True"
                        IsGroupingEnabled="true"
                        Margin="0,0,0,60"
                        VerticalOptions="FillAndExpand"
                        AbsoluteLayout.LayoutFlags="All"
                        AbsoluteLayout.LayoutBounds="0,0,1,1"
                        CachingStrategy="RecycleElement"
                        ItemSelected="PanierListView_ItemSelected">

                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout BackgroundColor="LightGray">
                                    <Label Text="{Binding Key}" Margin="0,6,0,0" HorizontalOptions="Center" VerticalOptions="Center" FontSize="Small" HeightRequest="30" VerticalTextAlignment="Center" />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>

                    <!--Built in Cells-->
                    <ListView.ItemTemplate>
                        <DataTemplate>

                            <ViewCell>

                                <SwipeView>
                                    <SwipeView.LeftItems>
                                        <SwipeItems>
                                            <SwipeItem 
                                                Text="Edit"
                                                IconImageSource="edit.png"
                                                BackgroundColor="DarkSeaGreen"
                                                Invoked="OnEditSwipeItem_Invoked" />
                                        </SwipeItems>
                                    </SwipeView.LeftItems>

                                    <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="45*" />
                                        <ColumnDefinition Width="8*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="60" />
                                    </Grid.RowDefinitions>
                                    <StackLayout Orientation="Horizontal" VerticalOptions="Center" Padding="3" Grid.Column="0">
                                        <Image Source="{Binding ImageArt, Converter={StaticResource ImageConverter}}" 
                                               Margin="10,0,0,0"
                                               WidthRequest="45" 
                                               HeightRequest="45"
                                               VerticalOptions="Start"
                                               Grid.Column="0"/>
                                        <StackLayout Orientation="Vertical">
                                            <Label Text="{Binding Name}" 
                                               Margin="10,-4,0,0" FontSize="{Binding NameFontSize}" TextColor="Black" 
                                               FontAttributes="None"
                                               VerticalOptions="Start"
                                               MaxLines="1"
                                               LineBreakMode="TailTruncation"
                                               Grid.Column="0"/>
                                            <Grid Margin="0,0,0,0">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="280*" />
                                                    <ColumnDefinition Width="70*" />
                                                </Grid.ColumnDefinitions>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="30" />
                                                </Grid.RowDefinitions>
                                                <StackLayout Orientation="Horizontal" Margin="0,-5,0,0" Grid.Column="0">
                                                    <Label Text="{Binding QteArt}" Margin="10,0,0,0" />
                                                    <Label Text="{Binding UniteMesure}" />
                                                    <Label Text="{Binding Separator}" />
                                                    <Label Text="{Binding Note}" LineBreakMode="TailTruncation" MaxLines="1"/>
                                                </StackLayout>
                                                <syncfusion:SfNumericTextBox
                                                    x:Name="totalart"
                                                    Value="{Binding TotalArt}" 
                                                    IsVisible="{Binding TotalVisible}"
                                                    MaximumNumberDecimalDigits="2"
                                                    FormatString="0.00 €"
                                                    Margin="0,-15,0,0"
                                                    HorizontalOptions="End"
                                                    VerticalOptions="Start"
                                                    IsReadOnly="True"
                                                    FontSize="Small"
                                                    Grid.Column="1"
                                                />
                                            </Grid>
                                        </StackLayout>
                                    </StackLayout>
                                    <input:CheckBox Grid.Column="1"
                                                    x:Name="chkBoxPanierOut"
                                                    IsChecked="{Binding IsChk, Mode=TwoWay}"
                                                    Type="Check"
                                                    Color="Green"
                                                    BoxSizeRequest="28"
                                                    BindingContext="{Binding .}"
                                                    VerticalOptions="Center"
                                                    Margin="10,0,20,0"
                                                    CheckChanged="chkBoxPanierOut_CheckChanged">
                                    </input:CheckBox>
                                </Grid>

                                </SwipeView>

                            </ViewCell>

                            
                        </DataTemplate>
                    </ListView.ItemTemplate>

                    <ListView.Footer>
                        <StackLayout Orientation="Vertical">
                            <Label Text=" "/>
                            <Label Text=" "/>
                            <Label Text=" "/>
                        </StackLayout>
                    </ListView.Footer>
                </ListView>

                

                <Grid HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
                    <Image x:Name="ImgListEmpty" 
                               Opacity="0.7" 
                               IsVisible="false" 
                               Aspect="Fill"
                               Source="Avertissement_7.png"/>
                    <Label x:Name="AdvertListEmpty1"
                               IsVisible="false"
                               FontSize="Medium"
                               TextColor="White"
                               FontAttributes="Italic"
                               Text="{helpers:Translate Panier_achatsPage_Avert01}"
                               VerticalOptions="Center"
                               HorizontalOptions="Center"
                               Margin=" 0,-30,0,0"/>
                    <Label x:Name="AdvertListEmpty2"
                               IsVisible="false"
                               FontSize="Medium"
                               TextColor="White"
                               FontAttributes="Italic"
                               Text="{helpers:Translate Panier_achatsPage_Avert02}"
                               VerticalOptions="Center"
                               HorizontalOptions="Center"
                               Margin=" 0,30,0,0"/>
                    <Image x:Name="ImgCartFull" 
                               Opacity="0.7" 
                               IsVisible="false" 
                               Aspect="Fill"
                               Source="Avertissement_6.png"/>
                    <Label x:Name="AdvertCartFull"
                               IsVisible="false"
                               FontSize="Medium"
                               TextColor="White"
                               FontAttributes="Italic"
                               Text="{helpers:Translate Panier_achatsPage_Avert03}"
                               VerticalOptions="Center"
                               HorizontalOptions="Center"/>
                    <Label x:Name="TextFavEmptyL1"
                               IsVisible="false"
                               FontSize="Medium"
                               TextColor="White"
                               FontAttributes="Italic"
                               Text="{helpers:Translate Panier_achatsPage_Avert04}"
                               VerticalOptions="Center"
                               HorizontalOptions="Center"
                               Margin=" 0,-30,0,0"/>
                    <Label x:Name="TextFavEmptyL2"
                               IsVisible="false"
                               FontSize="Medium"
                               TextColor="White"
                               FontAttributes="Italic"
                               Text="{helpers:Translate Panier_achatsPage_Avert05}"
                               VerticalOptions="Center"
                               HorizontalOptions="Center"
                               Margin=" 0,30,0,0"/>
                </Grid>

                <local:CircleButton 
                    FontIcon="Material"
                    Icon="add" 
                    FontSize="Large" TextColor="White" 
                    HeightRequest="60" WidthRequest="60"
                    CornerRadius="30"
                    HorizontalOptions="End"
                    VerticalOptions="End"
                    Margin="0,0,20,40"
                    Clicked="FabBtnAdd_clicked"
                    BackgroundColor="#5E91FF"
                    AbsoluteLayout.LayoutFlags="PositionProportional"
                    AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize">
                </local:CircleButton>

                <local:CircleButton x:Name="CircleBtnLock"
                    FontIcon="Material"
                    Icon="md-lock" 
                    FontSize="Medium" TextColor="White" 
                    HeightRequest="30" WidthRequest="30"
                    CornerRadius="30"
                    HorizontalOptions="End"
                    VerticalOptions="End"
                    Margin="0,0,110,55"
                    Clicked="CircleBtnLock_clicked"
                    BackgroundColor="IndianRed"
                    AbsoluteLayout.LayoutFlags="PositionProportional"
                    AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize">
                </local:CircleButton>

                <local:CircleButton x:Name="CircleBtnStopModif"
                    FontIcon="Material"
                    Icon="md-edit" 
                    FontSize="Medium" TextColor="White" 
                    HeightRequest="30" WidthRequest="30"
                    CornerRadius="30"
                    HorizontalOptions="End"
                    VerticalOptions="End"
                    Margin="0,0,165,55"
                    Clicked="CircleBtnStopModif_Clicked"
                    BackgroundColor="IndianRed"
                    AbsoluteLayout.LayoutFlags="PositionProportional"
                    AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize">
                </local:CircleButton>

                <local:CircleButton x:Name="CircleBtnUnLockModif"
                        FontIcon="Material"
                        Icon="md-edit" 
                        IsVisible="false"
                        FontSize="Medium" TextColor="White" 
                        HeightRequest="30" WidthRequest="30"
                        CornerRadius="30"
                        HorizontalOptions="End"
                        VerticalOptions="End"
                        Margin="0,0,165,55"
                        Clicked="CircleBtnUnLockModif_Clicked"
                        BackgroundColor="YellowGreen"
                        AbsoluteLayout.LayoutFlags="PositionProportional"
                        AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize">
                </local:CircleButton>

                <Label  Text="{helpers:Translate Panier_achatsPage_L01}" 
                            x:Name="totaltxt"
                            Margin="75,0,0,20"
                            VerticalOptions="End"
                            AbsoluteLayout.LayoutFlags="PositionProportional"
                            AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize"
                            FontSize="Medium" 
                            FontAttributes="Bold" 
                            IsVisible="true"/>

                <Label  x:Name="totalAchats"
                            Text="" 
                            Margin="145,0,0,20"
                            VerticalOptions="End"
                            AbsoluteLayout.LayoutFlags="PositionProportional"
                            AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize"
                            FontSize="Medium" 
                            FontAttributes="Bold" 
                            IsVisible="true"/>

                <Label  Text=" €"
                            x:Name="devisetxt"
                            Margin="210,0,0,20"
                            VerticalOptions="End"
                            AbsoluteLayout.LayoutFlags="PositionProportional"
                            AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize"
                            FontSize="Medium" 
                            FontAttributes="Bold" 
                            IsVisible="true"/>


                <Grid HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
                    <Image x:Name="fondAI" 
                               Opacity="0.8" 
                               IsVisible="true" 
                               Aspect="Fill"
                               Source="FondProgressRing.png"/>
                    <local:CircleButton x:Name="CircleBtnUnLock"
                        FontIcon="Material"
                        Icon="md-lock-open" 
                        IsVisible="true"
                        FontSize="Medium" TextColor="White" 
                        HeightRequest="30" WidthRequest="30"
                        CornerRadius="30"
                        HorizontalOptions="End"
                        VerticalOptions="End"
                        Margin="0,0,110,55"
                        Clicked="CircleBtnUnLock_clicked"
                        BackgroundColor="Green"
                        AbsoluteLayout.LayoutFlags="PositionProportional"
                        AbsoluteLayout.LayoutBounds="1, 1, AutoSize, AutoSize">
                    </local:CircleButton>

                    

                </Grid>

            </Grid>
        </AbsoluteLayout>


    </ContentPage>

这是操作FabBtnAdd_clicked的代码隐藏

private async void FabBtnAdd_clicked(object sender, EventArgs e)
    {
        DB_ListeAchatsList = dblisteachats.GetAllListeAchats();
        var listeachats = DB_ListeAchatsList.Where(x => x.NomListe == App.currentListeAchat).FirstOrDefault();
        //
        var artpanierpage = new ArtPanierPage();
        artpanierpage.BindingContext = listeachats;
        await Navigation.PushAsync(artpanierpage);
    }

不幸的是,我在运行它时遇到了问题。当 Listview 为空时,将打开 XAML 页,我可以通过点击 + 按钮来添加项。但是,如果存在项目,程序将崩溃而不会显示错误消息。退出窗口中只有以下内容:

[libc]致命信号 11 (SIGSEGV)、代码 1 (SEGV_MAPERR)、故障地址0x4 TID 12730 (oft.memocourses)、pid 12730 (oft.memocourses)

我在网站上找到了其他开发人员给出的示例,总的来说,它看起来像我的代码。我不明白发生了什么。谢谢你的帮助。

C# Android ListView Xamarin Xamarin.Forms

评论

0赞 Shaw 8/8/2023
请同时发布 xaml.cs 代码。崩溃究竟是什么时候发生的,在加载时,在点击项目时,在滑动时或其他一些操作时?如果删除滑动视图部分,列表视图是否有效?
0赞 Marcel Delhaye 8/9/2023
一旦页面打开(加载时),当列表视图中有 1 个项目时,就会发生崩溃。如果我删除与 SwipeItem 对应的代码,一切正常。我用整个页面替换了 XAML 代码。
0赞 Liqun Shen-MSFT 8/9/2023
请问您如何实现事件处理程序。如果可能,还可以在代码隐藏和视图模型中共享相关代码。FabBtnAdd_clicked
0赞 Marcel Delhaye 8/10/2023
@Liqun:我编辑了我的问题,添加了操作FabBtnAdd_clicked的代码隐藏。谢谢你的帮助。
0赞 Liqun Shen-MSFT 8/10/2023
您可以尝试设置断点并找出导致此错误的行。FabBtnAdd_clicked

答:

0赞 Marcel Delhaye 8/17/2023 #1

我将 Xamarin.Essentials 更新到版本 1.7.0,将 Xamarin.Forms 更新到版本 5.0.0.2196,从那时起,应用不再崩溃,并且 SwipeView 可以正常工作(SwipeView 命令除外,该命令不再接受 Threshold 参数)。该更新还产生了其他问题,但由于它没有带回 Swipeview,所以我会问一个新问题。谢谢。