【问题标题】:[Xamarin.Forms]Custom control in ListView[Xamarin.Forms]ListView中的自定义控件
【发布时间】:2021-04-20 19:26:58
【问题描述】:

我在使用 Binding 在 Listview 中使用自定义控件时遇到问题。 我给你我的代码,让它更具体。

我的自定义控件(FridgeControl)

Xaml:

<ContentView.Content>
  <StackLayout>
     <Label x:Name="LabelName" Text="{Binding Source={x:Reference FridgeView}, Path=NameFridge }" />
  </StackLayout>
</ContentView.Content>    

Cs:

public partial class FridgeControl : ContentView
{       
    public string NameFridge { get; set; }

    public static readonly BindableProperty NameFridgeProperty = BindableProperty.Create(
                                                     propertyName: "NameFridge",
                                                     returnType: typeof(string),
                                                     declaringType: typeof(FridgeControl),
                                                     defaultValue: "");

    public FridgeControl ()
    {
        InitializeComponent ();
    }
}

我的页面(FridgePage)

Xaml:

 <ContentPage.Content>
    <StackLayout>
        <ListView x:Name="ListFridges"
                  ItemsSource="{Binding Fridges}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <c:FridgeControl NameFridge="{Binding Name}"></c:FridgeControl>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
 </ContentPage.Content>

Cs:

public partial class FridgesPages : ContentPage
{
    public ObservableCollection<Fridge> Fridges { get; private set; }

    public FridgesPages()
    {
        InitializeComponent();

        this.Fridges = new ObservableCollection<Fridge>()
        {
            new Fridge
            {
                Id = 1,
                Name = "Garage",
                Foods = new List<Food>()
                {
                    new Food
                    {
                        Id = 14,
                        Name = "Tomatoes",
                        Type = Models.Enum.TypeFoodEnum.Vegetal,
                        MaxDate = new DateTime(2021, 01, 20)
                    },
                    new Food
                    {
                        Id = 6,
                        Name = "Chicken",
                        Type = Models.Enum.TypeFoodEnum.Meat,
                        MaxDate = new DateTime(2021, 01, 15)
                    },
                    new Food
                    {
                        Id = 44,
                        Name = "Water",
                        Type = Models.Enum.TypeFoodEnum.Drink,
                    },
                }
            },
            new Fridge
            {
                Id = 1,
                Name = "Kitchen",
                Foods = new List<Food>()
                {
                    new Food
                    {
                        Id = 14,
                        Name = "Salad",
                        Type = Models.Enum.TypeFoodEnum.Vegetal,
                        MaxDate = new DateTime(2021, 01, 20)
                    },
                    new Food
                    {
                        Id = 6,
                        Name = "Meat",
                        Type = Models.Enum.TypeFoodEnum.Meat,
                        MaxDate = new DateTime(2021, 01, 15)
                    },
                    new Food
                    {
                        Id = 44,
                        Name = "Juice",
                        Type = Models.Enum.TypeFoodEnum.Drink,
                    },
                }
            }
        };

        BindingContext = this;
    }
}

对象冰箱:

public class Fridge
{
    public int Id { get; set; }

    public string Name { get; set; }

    public List<Food> Foods { get; set; }

    public Fridge() { }
}

当我进入 FridgePAge 页面时,我遇到了 Cast Exception。 我应该怎么做才能在 ListView 中使用我的自定义控件?

你能帮我找出我做错了什么吗?

谢谢。

【问题讨论】:

  • 你的 Fridge 类是什么样子的——它有一个属性 Name 被定义为一个字符串吗?
  • 如果数据寺庙部分可以有自己的模板,为什么还要在数据模板中使用自定义控件
  • 感谢您的回答。 @Russ:我添加了冰箱对象。它确实有一个名为 Name 的属性
  • @Hammad Shabbir 我不明白你的回答。请问我应该如何正确地做呢? ListView 不直接接受孩子。

标签: c# xamarin.forms


【解决方案1】:

将您的内容视图代码更改为此


<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Jobu.Forms.Views.FridgeControl">
  <ContentView.Content>
      <StackLayout>
            <Label x:Name="LabelName" Text="{Binding Name}" />
      </StackLayout>
  </ContentView.Content>
</ContentView>

并将代码后面的代码更改为此

   public partial class FridgePage : ContentPage
    {

        public ObservableCollection<Fridge> Fridges { get; private set; }

        public FridgePage()
        {
            InitializeComponent();

            this.Fridges = new ObservableCollection<Fridge>(){
            new Fridge
            {
                Id = 1,
                Name = "Garage",
                Foods = new List<Food>()
                {
                    new Food
                    {
                        Id = 14,
                        Name = "Tomatoes",
                        Type = Models.Enum.TypeFoodEnum.Vegetal,
                        MaxDate = new DateTime(2021, 01, 20)
                    },
                    new Food
                    {
                        Id = 6,
                        Name = "Chicken",
                        Type = Models.Enum.TypeFoodEnum.Meat,
                        MaxDate = new DateTime(2021, 01, 15)
                    },
                    new Food
                    {
                        Id = 44,
                        Name = "Water",
                        Type = Models.Enum.TypeFoodEnum.Drink,
                    },
                }
            }
        };

        BindingContext = this;
        }
    }

将您的内容页面代码更改为此

    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="ListFridges"
                      ItemsSource="{Binding Fridges}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <c:FridgeControl NameFridge="{Binding Name}"></c:FridgeControl>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>

【讨论】:

  • 并非如此。因为目标是使用自定义控件(FridgeControl)以便以后能够在应用程序的其他地方重用它,而不必重写相同的代码。
  • 另外,我必须告诉你,我一开始放的代码中的控件(FridgeControl)是不完整的。一旦它与标签/属性一起使用,我必须丰富它
  • 因此您可以在数据模板中定义布局,并将数据模板用作静态资源并在整个应用程序中使用
  • 但是我已经用自定义控件更新了我的答案。试试看
  • 感谢您的回答。现在我不再有“Cast exception”错误。但是包含 2 个项目的 ListView 是空的。绑定似乎不起作用。我真的不明白为什么......
猜你喜欢
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 2018-03-03
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多