【问题标题】:Xamarin Forms - Binding Multiple TextCells in one ListViewXamarin Forms - 在一个 ListView 中绑定多个 TextCell
【发布时间】:2014-09-23 00:50:12
【问题描述】:

我无法在 ListView 中绑定多个 TextCell。如果只有一个,它可以正常工作,但在添加更多时会给出 XamlParseException。尝试绑定标签时发生相同的异常。这就是我必须使用 TextCell 的原因。解决办法是什么?

<ListView x:Name="pList">
    <ListView.ItemTemplate>
      <DataTemplate>
        <TextCell x:Name="a" Text="{Binding ReceiverName}" TextColor="White" />
      </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: c# xaml listview binding xamarin


    【解决方案1】:

    从您对其中一个答案的评论看来,这就是您想要的

        <ListView x:Name="pList">
            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                  <ViewCell.View>
                    <StackLayout>
                      <Label Text="{Binding ReceiverName}" TextColor="White" />
                      <Label Text="{Binding SecondText}" TextColor="White" />
                      <Label Text="{Binding ThirdText}" TextColor="White" />
                    </StackLayout>
                  </ViewCell.View>          
                 </ViewCell>
               </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    这将垂直显示 3 个标签。您遇到的问题是 DataTemplate 不能有多个子级。解决这个问题的标准方法是使用布局控件,例如 StackLayout。

    更多信息请查看此页面:http://developer.xamarin.com/guides/cross-platform/xamarin-forms/controls/layouts/

    【讨论】:

      【解决方案2】:

      从您粘贴的代码中猜测,我会说问题在于您为控件命名。删除a:Name 并重试。

      如果这没有帮助,请同时发布异常详细信息。

      【讨论】:

      • 删除 Name 属性没有任何区别。尝试在第一个 TextCell 正下方添加另一个 TextCell 时出现此异常 - System.Windows.ni.dll 中发生“System.Reflection.TargetInvocationException”类型的未处理异常我的问题不在于给定的代码,它是在我添加时出现的另一个 TextCell 就在第一个下方。
      • 啊,是的 - 您需要在视图中添加文本单元格。尝试用 &lt;StackLayout /&gt; 包围你的两个单元格
      【解决方案3】:

      您需要在数据模板中添加“ViewCell”,如下所示:

      <ListView x:Name="pList">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <ViewCell.View>
                <TextCell x:Name="a" Text="{Binding ReceiverName}" TextColor="White" />
              </ViewCell.View>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
      

      【讨论】:

      • 其实我想做的是 -
      【解决方案4】:

      TextCell 也有一个 detail 属性:

      <TextCell x:Name="a" Text="{Binding ReceiverName}" Detail="{Binding AnotherName}" TextColor="White" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        • 2017-10-03
        • 2018-01-17
        • 1970-01-01
        相关资源
        最近更新 更多