首页 > 解决方案 > 在 Xamarin Forms 中添加新的“注释”时如何将 SwipeView 添加到 CollectionView

问题描述

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

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

我在 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>

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

标签: c#xamarin.forms

解决方案


您可能在指定时有错误command

在数据模板中

尝试像这样修复:

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

ItemTapped是你的命令在你的viewmodel.


推荐阅读