【问题标题】:Xamarin: Binding a nested collection object to ListViewXamarin:将嵌套集合对象绑定到 ListView
【发布时间】:2022-06-11 11:17:14
【问题描述】:

请多多包涵,我是 Xamarin 新手

我有这个对象 Job 和一个子对象列表 Process

public class Job
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public DateTime Date { get; set; }
        public IEnumerable<Process> Processes { get; set; }
    }

public class Process
    {
        public string Id { get; set; }
        public string Description { get; set; }
        public string Procedure { get; set; }
    }

在 xaml 页面中,我有一个 SwipeView,它显示所有带有 Expander 的作业 Titles,在其中我可以为每个 job 显示 Job.Date。但我的问题是在ListView 中显示Processes 列表。

ViewModel(为简洁起见)

internal class MyJobsPageVM : BaseViewModel
    {
        public ObservableCollection<Job> MyJobs { get; }
        
        public MyJobsPageVM()
        {
            MyJobs = new ObservableCollection<CompJob>();
        }
        

        /* ... Code to load jobs with Rest Service into MyJobs ObservableCollection */
    }

XAML 页面(为简洁起见)

    <ContentPage.BindingContext>
        <vm:MyJobsPageVM />
    </ContentPage.BindingContext>
                <CollectionView x:Name="JobsListSwipeCollectionView"
                                   ItemsSource="{Binding MyJobs}">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <SwipeView>
                                <SwipeView.LeftItems>
                                    <SwipeItems Mode="Reveal" >
                                        <SwipeItem Text="Done"/>
                                    </SwipeItems>
                                </SwipeView.LeftItems>
                                <extensions:Expander>
                                    <extensions:Expander.Header>
                                         <Label>
                                                <Label.Text>
                                                    <MultiBinding>
                                                        <Binding Path="Title" />
                                                    </MultiBinding>
                                                </Label.Text>
                                         </Label>
                                    </extensions:Expander.Header>
                                    <StackLayout>
                                      <Label>
                                        <Label.Text>
                                            <MultiBinding>
                                                <Binding Path="Date" />
                                            </MultiBinding>
                                        </Label.Text>
                                      </Label>
<!-- This is where I am stuck -->    <ListView ItemsSource="{Binding path=Processes}">
                                        <ListView.ItemTemplate>
                                            <DataTemplate>
                                                <ViewCell>
                                                    <Label>
                                                        <Label.Text>
                                                            <MultiBinding>
                                                                <Binding Path="Description"/>
                                                            </MultiBinding>
                                                        </Label.Text>
                                                    </Label>
                                                </ViewCell>
                                            </DataTemplate>
                                        </ListView.ItemTemplate>
                                      </ListView>
                                    </StackLayout>
                                </extensions:Expander>
                           </SwipeView>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </StackLayout>

【问题讨论】:

  • 一般来说,嵌套的 ListView 是个坏主意。但是,您所拥有的似乎是正确的,理论上应该可以工作。
  • 我不知道大小写是否重要,但请尝试="{Binding Processes}"="{Binding Path=Processes}"。顺便说一句,你没有说出了什么问题。语法错误,还是空列表?如果列表为空,您是否在输出中查看是否有任何相关警告?
  • @ToolmakerSteve 没有错误或任何东西,只是没有显示值。我刚刚意识到问题是由RefreshView 引起的,为了保持简单,我在帖子中省略了该RefreshView 我删除了它,一切正常。是 DataType 搞乱了其余的绑定吗?
  • 如果 DataType 导致问题,您应该在输出中看到警告。通常,一旦您在 xaml 中声明了数据类型,如果应用了不同的数据类型,则该 xaml 元素“内部”的任何 xaml 嵌套也需要具有 DataType 声明。

标签: c# xamarin xamarin.forms xamarin.android


【解决方案1】:

您可以使用BindableLayout 显示项目集合,而无需将其包装在 CollectionView 中。

可以参考以下代码:

   <ContentPage.BindingContext>
    <form0530:MyJobsPageVM></form0530:MyJobsPageVM>
</ContentPage.BindingContext>

<StackLayout>
    <CollectionView x:Name="JobsListSwipeCollectionView"
                               ItemsSource="{Binding MyJobs}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                        <StackLayout>
                            <Label Text="{Binding Title}">

                            </Label>

                            <!-- add code here -->

                    <StackLayout  BindableLayout.ItemsSource="{Binding Processes}">
                        <BindableLayout.ItemTemplate>
                            <DataTemplate>
                                <Label Text="{Binding Description}"/>
                            </DataTemplate>
                        </BindableLayout.ItemTemplate>
                    </StackLayout>

                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>

【讨论】:

  • 我刚刚意识到问题是由RefreshView 引起的,我在帖子中为了保持简单而省略了它。 &lt;RefreshView x:DataType="vm:MyJobsPageVM" Command="{Binding LoadJobRequestsCommand}"&gt; 我删除了它,一切正常。是 DataType 搞乱了其余的绑定吗?
【解决方案2】:

问题是由我在帖子中为了保持简单而省略的 RefreshView 引起的。我删除了它,一切正常。 我会尝试找到更好的解决方案来重新实现 RefreshView 功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多