【问题标题】:How do I add a SwipeView to CollectionView when adding a new "Note" in Xamarin Forms在 Xamarin Forms 中添加新的“注释”时如何将 SwipeView 添加到 CollectionView
【发布时间】:2021-07-20 19:39:26
【问题描述】:

我目前正在为一个学校项目制作一个笔记应用程序。我没有太多经验,请见谅。

当新项目添加到“笔记”列表中时,我想向我的 CollectionView 添加一个 SwipeView,这将允许用户删除笔记。

我使用了 xamarin 中的默认弹出布局来帮助我。

ItemsPage 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"
             x:Class="xamarinMobileTest.Views.ItemsPage"
             Title="{Binding Title}"
             xmlns:local="clr-namespace:xamarinMobileTest.ViewModels"  
             xmlns:model="clr-namespace:xamarinMobileTest.Models"  
             x:Name="BrowseItemsPage">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add Note" Command="{Binding AddItemCommand}" />
    </ContentPage.ToolbarItems>

    <RefreshView x:DataType="local:ItemsViewModel" Command="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
        <CollectionView x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems Mode="Execute">
                                <SwipeItem Text="Delete" 
                                           BackgroundColor="Red"
                                           Invoked="SwipeItem_Invoked"/>
                            </SwipeItems>
                        </SwipeView.RightItems>
                        <StackLayout Padding="5" x:DataType="model:Item">
                            <Label Text="{Binding Text}"
                                LineBreakMode="NoWrap" 
                                Style="{DynamicResource ListItemTextStyle}" 
                                FontSize="16"
                                FontAttributes="Bold"
                                TextColor="Black"/>
                            <Label Text="{Binding Description}" 
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemDetailTextStyle}"
                                FontSize="13"
                                TextColor="Black"/>
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer 
                                NumberOfTapsRequired="1"
                                Command="{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=ItemTapped}"     
                                CommandParameter="{Binding .}">
                                </TapGestureRecognizer>
                            </StackLayout.GestureRecognizers>
                        </StackLayout>
                    </SwipeView>
                </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
    </RefreshView>
</ContentPage>

这是我到目前为止所拥有的,当尝试保存笔记时,应用程序崩溃了。 任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 我创建了一个 Shell 项目以使用 SwipeView 进行测试。没有错误。请查看我的截图:imgur.com/0pBCrLk您可以创建一个新的Shell项目再次查看。
  • 你能分享你的代码吗?每当我将 SwipeView 添加到我的 CollectionView 时,当我单击新项目中的浏览选项卡时,我都会收到一个异常。 System.Reflection.TargetInvocationException: '调用的目标抛出了异常。'
  • 我已经发布了代码。但我认为它是相似的。查看截图:imgur.com/o9DchtU

标签: c# xamarin.forms


【解决方案1】:

您可能在指定command 时出错

在数据模板中

尝试像这样修复:

  Command="{Binding Source={x:Reference BrowseItemsPage},Path=ItemTapped"

ItemTapped 是您在viewmodel 中的命令。

【讨论】:

    【解决方案2】:

    带有Shell Template并添加SwipeView的原始代码:

     <RefreshView x:DataType="local:ItemsViewModel" Command="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
        <CollectionView x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView>
                        <SwipeView.RightItems>
                            <SwipeItems Mode="Execute">
                                <SwipeItem Text="Delete" 
                                           BackgroundColor="Red"
                                           Invoked="SwipeItem_Invoked"/>
                            </SwipeItems>
                        </SwipeView.RightItems>
                        <StackLayout Padding="10" x:DataType="model:Item">
                            <Label Text="{Binding Text}" 
                            LineBreakMode="NoWrap" 
                            Style="{DynamicResource ListItemTextStyle}" 
                            FontSize="16" />
                            <Label Text="{Binding Description}" 
                            LineBreakMode="NoWrap"
                            Style="{DynamicResource ListItemDetailTextStyle}"
                            FontSize="13" />
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer 
                                NumberOfTapsRequired="1"
                                Command="{Binding Source={RelativeSource AncestorType={x:Type local:ItemsViewModel}}, Path=ItemTapped}"     
                                CommandParameter="{Binding .}">
                                </TapGestureRecognizer>
                            </StackLayout.GestureRecognizers>
                        </StackLayout>
                    </SwipeView>
                    
                  
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView>
    

    我已经检查了你的代码。它仍然有效。

    【讨论】:

    • 很奇怪。我已经在两个单独的原始 shell 模板上测试了您的代码,一个在弹出模板上,另一个在选项卡式模板上,每当我打开带有实现代码的“浏览”选项卡时,应用程序就会崩溃。我不知道这是我的问题还是什么?
    【解决方案3】:

    我找到了解决办法:

    我需要将这段代码添加到我的 app.xamls.cs 中

        Device.SetFlags(new string[] { "SwipeView_Experimental", "AppTheme_Experimental" });
    

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 2022-07-28
      • 1970-01-01
      相关资源
      最近更新 更多