首页 > 解决方案 > 以编程方式填充 XAML ListView ItemsSource

问题描述

我正在尝试创建一个 Xamarin 跨平台应用程序。我选择了一个模板,现在我有一个 MainPage 和 ItemsPage。我认为 ItemsPage 是由 MainPage 调用的。

我在 ItemsPage.xaml 中有这段代码

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="App1.Views.ItemsPage"
             Title="{Binding Title}"
             x:Name="BrowseItemsPage">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Clicked="AddItem_Clicked" />
    </ContentPage.ToolbarItems>

    <StackLayout>
        <ListView x:Name="ItemsListView"
                ItemsSource="{Binding Items}"
                VerticalOptions="FillAndExpand"
                HasUnevenRows="true"
                RefreshCommand="{Binding LoadItemsCommand}"
                IsPullToRefreshEnabled="true"
                IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                CachingStrategy="RecycleElement"
                ItemSelected="OnItemSelected">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="10">
                            <Label Text="{Binding Text}" 
                                d:Text="{Binding .}"
                                LineBreakMode="NoWrap" 
                                Style="{DynamicResource ListItemTextStyle}" 
                                FontSize="16" />
                            <Label Text="{Binding Description}" 
                                d:Text="Item descripton"
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemDetailTextStyle}"
                                FontSize="13" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

    </ContentPage>

和 ItemsPage.cs 中的这段代码

    public ItemsPage()
    {
            InitializeComponent();

            BindingContext = viewModel = new ItemsViewModel();
            string[] files = folderEntryListinAssets("folderName");

            ObservableCollection<String> entries = new ObservableCollection<String>();

            for (var i=0;i<files.Length;i++)
            {
               Console.WriteLine("app output - entry "+files[i]);
               entries.Add(files[i]);
            }
            ItemsListView.ItemsSource = entries;           
        }


    private String[] folderEntryListinAssets(String folderName)
    {                           
                switch (Device.RuntimePlatform)
                {
                    case Device.Android:
                    Context context = Android.App.Application.Context;

                    String[] list=context.Assets.List(folderName);
                    Console.WriteLine("app output - list size " + list.Length);
                    for (int i = 0; i < list.Length; i++)
                    { Console.WriteLine("app output - entry "+list.GetValue(i).ToString()); }


                    return list;
                   /* case Device.UWP:
                        path = "ms-appx-web:///";
                        return path;
                case Device.iOS:
                    path = NSBundle.MainBundle.BundlePath;
                    return path;*/
                    default:

                    return null;
                }

我什么也没看见。

请注意,我删除了 XAML 中的默认字符串,它们已正确显示。

现在我需要以编程方式设置 ItemsSource,如上所示。

我看到这些条目在控制台日志中被创建为字符串。

我的代码有什么问题?

标签: c#listviewxamarindata-bindingcross-platform

解决方案


The problem is the way you set ItemSource and bindings in viewcell. According to your viewcell, it expects each item to be a model in which there are two properties: Text and Description, i.e the ItemSourse must be collection of that model.

You should have a model like this:

public class MyModel{
    public string Text{get;set;}
    public string Description{get;set;}
}

And you need to set ItemSource like this:

ObservableCollection<MyModel> entries = new ObservableCollection<MyModel>();

for (var i = 0; i<files.Length; i++)
{
Console.WriteLine("app output - entry " + files[i]);

var model = new MyModel
{
    Text = "text value",
    Description = "description value"
};

entries.Add(model);
}
ItemsListView.ItemsSource = entries;

Finally it depends on you how to fill each model i.e Text and Description.


推荐阅读