【发布时间】: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