Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示简单列表的Xamarin表单_Xamarin_Xamarin.forms_Xamarin.ios - Fatal编程技术网

显示简单列表的Xamarin表单

显示简单列表的Xamarin表单,xamarin,xamarin.forms,xamarin.ios,Xamarin,Xamarin.forms,Xamarin.ios,我有一个对象列表,我只想为每个项目显示几个标签 在asp.net时代,我会使用带有标签的转发器 我意识到在Xamarin形式中没有转发器,或者说是吗 我尝试使用Listview,但它不起作用,因为在IOS中,它会创建空行,直到屏幕结束 是否有ListView的替代方案 这就是我们的想法: <repeater:RepeaterView ItemsSource="{Binding MyObject}"> <Label Text="

我有一个对象列表,我只想为每个项目显示几个标签

在asp.net时代,我会使用带有标签的转发器

我意识到在Xamarin形式中没有转发器,或者说是吗

我尝试使用Listview,但它不起作用,因为在IOS中,它会创建空行,直到屏幕结束

是否有ListView的替代方案

这就是我们的想法:

<repeater:RepeaterView 
          ItemsSource="{Binding MyObject}">

              <Label Text="{Binding Prop1 }"/>
               <Label Text="{Binding Prop2 }"/>

 </repeater:RepeaterView>

如何在Xamarin表单中实现这一点

是否有一种本机方式来实现这一点,或者仅使用第三方库


多亏了您可以使用名为
RepeaterView
的自定义控件来实现这一点,有两个类似的实现,但是Xamarin MVPHoussem Dellai为如何使用
RepeaterView
提供了一个很好的示例,以及如何在其中包含点击事件的示例

以下是XAML用法片段:

 <repeater:RepeaterView ItemsSource="{Binding YourCollection}">

            <repeater:RepeaterView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid Padding="20"
                              Margin="10">

                            <Grid.GestureRecognizers>
                                <TapGestureRecognizer Tapped="Student_Tapped" />
                            </Grid.GestureRecognizers>

                            <Label Text="{Binding SomePropertyOfYourModel}"
                                   FontSize="36" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </repeater:RepeaterView.ItemTemplate>
        </repeater:RepeaterView>

您可以在他的GitHub页面上找到完整的示例:

他使用的Nuget:

还有Youtube视频,他解释道:

2019年12月31日编辑:

表单现在支持可绑定的布局,所以您可以看看官方对这种行为的支持

我尝试使用Listview,但它不起作用,因为在IOS中,它会创建空行,直到屏幕结束

正如Vahid所说,我们可以创建自定义渲染器来解决这个问题

[assembly: ExportRenderer(typeof(ListView), typeof(CustomListViewRenderer))]
namespace twoWayBinding.iOS
{
    class CustomListViewRenderer : ListViewRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
                return;

            var tableView = Control as UITableView;
            tableView.TableFooterView = new UIView(frame: CGRect.Empty);
        }
    }
}
[程序集:ExportRenderer(typeof(ListView)、typeof(CustomListViewRenderer))]
命名空间twoWayBinding.iOS
{
类CustomListViewRenderer:ListViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control==null)
返回;
var tableView=控件作为UITableView;
tableView.TableFooterView=新UIView(框架:CGRect.Empty);
}
}
}

我尝试过使用Listview,但它不起作用,因为在IOS中它会创建 空行,直到屏幕结束

如果这是您不使用ListView的唯一原因,那么有一个简单的解决方案:在表单端插入一个空页脚。虽然Cole Xia的想法是正确的,但您可以在不使用自定义渲染器的情况下执行此操作:

<ListView ...>
  <ListView.Footer>
    <StackLayout></StackLayout>
  </ListView.Footer>

如果页脚为空,则得到观察到的空行。因此需要有非空的页脚,空的StackLayout可以避免空行。

已经提到了,但我想在单独的回答中强调它。不再需要使用自定义的“中继器”



我认为您可以通过编写自定义的iOS listview呈现程序来解决iOS listview问题。你可以找到一些解决这个问题的方法。谢谢你的帮助。我使用了另一个组件:我的应用程序在IOS设备上崩溃了。我会试试你的建议。@joseluisgonzalezclua为什么你要从我的答案中去掉“已回答”这个标志?我在2018年用这个解决方案回答了这个问题,并对Xamarin的新功能进行了编辑。表单也指BindableLayouts,我认为没有必要删除一个标志。。。你也可以接受另一个答案,并在这个问题上保持一致。非常感谢。实际上,我没有看到2019年关于可绑定布局的更新。对不起,阿尔米尔!谢谢,我最终使用了repeaterview组件,但这也是非常有用的。再见中继器
<StackLayout BindableLayout.ItemsSource="{Binding User.TopFollowers}"
             Orientation="Horizontal"
             ...>
    <BindableLayout.ItemTemplate>
        <DataTemplate>
            <controls:CircleImage Source="{Binding}"
                                  Aspect="AspectFill"
                                  WidthRequest="44"
                                  HeightRequest="44"
                                  ... />
        </DataTemplate>
    </BindableLayout.ItemTemplate>
</StackLayout>