所以我开始学习UWP编程并学习如何使用我使用此链接的滑动方法:Swipe Tutorial 但是在按照他们的步骤操作后,我开始收到以下错误:无法在索引0处添加子项。以及我们突出显示的以下代码行
<SwipeItems x:Key="RevealOptions" Mode="Reveal">
<SwipeItem Text="Touch it" IconSource="{StaticResource TouchIcon}"/>
<SwipeItem Text="Eat it" IconSource="{StaticResource EatIcon}"/>
</SwipeItems>
<SwipeItems x:Key="ExecuteBuying" Mode="Execute">
<SwipeItem Text="Buy it" IconSource="{StaticResource BuyIcon}"/>
</SwipeItems>
这里是完整的代码:
<Page.Resources>
<SymbolIconSource x:Key="TouchIcon" Symbol="View"/>
<SymbolIconSource x:Key="EatIcon" Symbol="Emoji"/>
<SymbolIconSource x:Key="BuyIcon" Symbol="Like"/>
<SwipeItems x:Key="RevealOptions" Mode="Reveal">
<SwipeItem Text="Touch it" IconSource="{StaticResource TouchIcon}"/>
<SwipeItem Text="Eat it" IconSource="{StaticResource EatIcon}"/>
</SwipeItems>
<SwipeItems x:Key="ExecuteBuying" Mode="Execute">
<SwipeItem Text="Buy it" IconSource="{StaticResource BuyIcon}"/>
</SwipeItems>
</Page.Resources>
<Grid>
<SwipeControl Name="MySwipeContainer"
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListView HorizontalAlignment="Center" VerticalAlignment="Center">
<ListViewItem Content="Apple" FontSize="36"/>
<ListViewItem Content="Banana" FontSize="36"/>
<ListViewItem Content="Grape" FontSize="36"/>
<ListViewItem Content="Lemon" FontSize="36"/>
<ListViewItem Content="Melone" FontSize="36"/>
<ListViewItem Content="Watermelon" FontSize="36"/>
</ListView>
</SwipeControl>
</Grid>
答案 0 :(得分:0)
从文档中,它将SwipeControl放在DataTemplate中,因此,您需要使用SwipeControl包装listviewitem而不是ListView。您的代码需要以下内容:
<ListView HorizontalAlignment="Center" VerticalAlignment="Center">
<SwipeControl
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListViewItem Content="Apple" FontSize="36"/>
</SwipeControl>
<SwipeControl
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListViewItem Content="Banana" FontSize="36"/>
</SwipeControl>
<SwipeControl
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListViewItem Content="Grape" FontSize="36"/>
</SwipeControl>
<SwipeControl
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListViewItem Content="Lemon" FontSize="36"/>
</SwipeControl>
<SwipeControl
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListViewItem Content="Melone" FontSize="36"/>
</SwipeControl>
<SwipeControl
LeftItems="{StaticResource RevealOptions}"
RightItems="{StaticResource ExecuteBuying}">
<ListViewItem Content="Watermelon" FontSize="36"/>
</SwipeControl>
</ListView>