带有命名空间的 ListView 中的数据绑定

时间:2021-04-26 10:50:16

标签: c# xaml xamarin data-binding

根据 Xamarin tutorial,我可以将 XAML 中的属性绑定到例如可观察的集合属性。

唯一的区别是,我正在为我的集合项类使用命名空间。

然而,在实施了我的简单版本的教程后,我最终得到了一个空列表。我目前正在使用一个简单的 ListView 来了解有关绑定的更多信息。

现在,我的模型很简单:

using Xamarin.Forms;

namespace Testapp.Models // This originally was NOT inside a namespace "Model"
{
    public class Item
    {
        public string Title;

        public string Description;

        public Image Icon;
    }
}

我的 C# 代码实现了一个返回 Items 的属性 ObservableCollection<Item>,就像教程一样,或者至少我看到的是:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Testapp.Models;
using Xamarin.Forms;

namespace Testapp.Pages
{
    public partial class ItemizedPage : ContentPage
    {
        ObservableCollection<Item> ItemsCollection = new ObservableCollection<Item>();

        public ObservableCollection<Item> Items { get { return ItemsCollection; } }

        public ItemizedPage()
        {
            InitializeComponent();

            ItemizedView.ItemsSource = Items;

            ItemsCollection.Add(new Item { Title = "Item One", Description = "Something" });
            ItemsCollection.Add(new Item { Title = "Thing Two", Description = "Someone" });
            ItemsCollection.Add(new Item { Title = "Whatever Three", Description = "Somewhere" });
        }
    }
}

相关的 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"
    x:Class="Testapp.Pages.ItemizedPage">

    <ContentPage.Content>
        <ListView x:Name="ItemizedView" ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
              <DataTemplate>
                <TextCell Text="{Binding Title}" />
              </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

据我所知,这应该可以正常工作,但事实并非如此。 ListView 为空。

我在这里错过了什么?模型 Item 位于文件夹内而 XAML 无法访问是不是有问题?

作为参考,这是 Visual Studio for Mac 8.9.7 build 8,带有 Xamarin Forms 5.0.0.2012。

1 个答案:

答案 0 :(得分:1)

$imageRoot = 'https://oddity.example.edu/uploads/postImages/"; ... $imageurl = $imageRoot . $row['imageslug']; echo '<img class="image" src="$imageurl" />; 中定义的变量更改为 Properties

Item
相关问题