【问题标题】:Failed to add the child at index 0未能在索引 0 处添加子项
【发布时间】:2018-04-05 18:00:42
【问题描述】:

所以我开始学习 UWP 编程并学习如何使用 Swipe 方法,我使用了这个链接: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>

【问题讨论】:

  • 我遇到了同样的问题 (Failed to add the child at index 0),我注意到我的解决方案配置设置为 ARM。我猜这发生在应用程序包的麻烦构建之后(用于将其部署到其他计算机上)。现在,我想知道为什么 ARM 会给出这个错误。毕竟 UWP 不是那么普遍吗?

标签: c# uwp swipe uwp-xaml


【解决方案1】:

从文档中,它将 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>

【讨论】:

  • 这是有道理的,但即使在使用您的代码后,我仍收到以下错误:资源“ExecuteBuying”的类型不兼容。资源“ RevealOptions" 具有不兼容的类型。
  • 您能在 Visual Studio 中构建和调试您的应用程序吗?我也面临这个问题,因为某些类是从 16299 引入的(例如 SymbolIconSource)。但是如果你在 16299 系统上运行你的应用程序,它不会影响你的应用程序。您的应用仍然可以成功运行。
  • 这个Failed to add the child at index 0 仍然是个问题。甚至当我运行应用程序时,我得到的是一个错位的视图,当我悬停时,滑动按钮会永久粘在项目的顶部。绝对不是应该发生的事情......难怪开发人员不为 UWP 编写应用程序(甚至不去研究整个框架的糟糕设计)。
猜你喜欢
  • 2011-08-18
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多