Xamarin Forms ListViewCell 不更新图像

Xamarin Forms ListViewCell not updating Image

我正在尝试让图像显示在我的自定义 ViewCell 中,但是,手动设置它不起作用。

我首先要创建一个自定义视图单元格列表,然后在其中设置图像。在我拥有我需要的所有视图单元格之后,我将它们添加到一个列表并将该列表设置为列表视图的 ItemSource。然而;即使应该通过一些非常简单的代码,图像也不会显示。我错过了什么吗?

以下是我正在加载视图单元格的 ContentPage。

public partial class InAppStorePage : ContentPage
{

    private List<ViewCell> cells;
    private Store inAppStore;

    public InAppStorePage()
    {
        InitializeComponent();
        InitializeObjects();
    }

    private void InitializeObjects()
    {
        cells = new List<ViewCell>();
        inAppStore = AppStore.CurrentStore;
    }

    protected override void OnAppearing()
    {
        SetListViewTemplate();
        LoadProductsIntoListView();
        SetListViewItemSource();
    }

    private void LoadProductsIntoListView()
    {
        LoadPurchasedProductsIntoListView();
        LoadPendingProductsIntoListView();
        LoadNonPurchasedProductsIntoListView();
    }

    private void SetListViewTemplate()
    {
        InAppProductsListView.ItemTemplate = new DataTemplate(typeof(InAppStoreViewCell));
    }

    private void LoadPurchasedProductsIntoListView()
    {
        List<ViewCell> purchasedProductCells = new List<ViewCell>();
        foreach (InAppProduct purchasedProduct in inAppStore.GetListOfPurchasedProducts())
        {
            InAppStoreViewCell purchasedProductViewCell = new InAppStoreViewCell();

            //The Line in Question
            purchasedProductViewCell.ProductImage.Source = purchasedProduct.GetIcon().Source;

            purchasedProductCells.Add(purchasedProductViewCell);
        }
        cells.AddRange(purchasedProductCells);
    }

    private void LoadPendingProductsIntoListView()
    {
        List<ViewCell> pendingPurchasedProductCells = new List<ViewCell>();
        foreach (InAppProduct pendingPurchaseProduct in inAppStore.GetListOfPendingPurchaseProducts())
        {
            InAppStoreViewCell pendingPurchaseProductCell = new InAppStoreViewCell();

            //The Line in Question
            pendingPurchaseProductCell.ProductImage.Source = pendingPurchaseProduct.GetIcon().Source;

            pendingPurchasedProductCells.Add(pendingPurchaseProductCell);
        }
        cells.AddRange(pendingPurchasedProductCells);
    }

    private void LoadNonPurchasedProductsIntoListView()
    {
        List<ViewCell> nonPurchasedProductCells = new List<ViewCell>();
        foreach (InAppProduct nonPurchasedProduct in inAppStore.GetListOfProductsThatHaventBeenPurchased())
        {
            InAppStoreViewCell nonPurchasedProductCell = new InAppStoreViewCell();

            //The Line in Question
            nonPurchasedProductCell.ProductImage.Source = nonPurchasedProduct.GetIcon().Source;

            nonPurchasedProductCells.Add(nonPurchasedProductCell);
        }
        cells.AddRange(nonPurchasedProductCells);
    }

    private void SetListViewItemSource()
    {
        InAppProductsListView.ItemsSource = null;
        InAppProductsListView.ItemsSource = cells;
    }

}

下面是自定义视单元的 C# 文件及其随附的 xaml 文件

    [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class InAppStoreViewCell : ViewCell
{
    public InAppStoreViewCell()
    {
        InitializeComponent();
    }

    public Image ProductImage
    {
        get
        {
            return CellProductIcon;
        } 

        set
        {
            CellProductIcon = value;
        }
    }

    public void SetColor(Color color)
    {
        ProductImage.BackgroundColor = color;
    }

    public Label ProductNameLabel
    {
        get
        {
            return CellProductNameLabel;
        }

        set
        {
            CellProductNameLabel = value;
        }
    }

    public Label ProductStatusLabel
    {
        get
        {
            return CellProductStatus;
        }

        set
        {
            CellProductStatus = value;
        }
    }

    public Label ProductPriceLabel
    {
        get
        {
            return CellProductPriceLabel;
        }

        set
        {
            CellProductPriceLabel = value;
        }
    }

}

Xaml 文件

<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Alan.Views.UIElements.InAppStoreViewCells.InAppStoreViewCell">
  <ViewCell.View>
      <RelativeLayout>
            <RelativeLayout x:Name="TopContainer"
                            BackgroundColor="CornflowerBlue"
                            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.75}">
                <Image x:Name="CellProductIcon"
                       BackgroundColor="Indigo"
                       Aspect="Fill"
                       RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1.0}"
                       RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=1.0}"/>
            </RelativeLayout>
            <RelativeLayout x:Name="BottomContainer"
                            BackgroundColor="Orange"
                            RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=1}"
                            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25}"
                            RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.75}">
                <Label x:Name="CellProductNameLabel"/>
                <Label x:Name="CellProductStatus"/>
                <Label x:Name="CellProductPriceLabel"/>
            </RelativeLayout>
        </RelativeLayout>
  </ViewCell.View>
</ViewCell>

任何帮助将不胜感激,因为这个超级简单的事情让我发疯:/

我觉得你的使用方式有问题。

例如:

1.You 没有为您的 ViewCell 设置 BindableProperty。

2.Why 子元素类型为 ViewCell 时,是否将 cells 赋值给 InAppProductsListView.ItemsSource

 private List<ViewCell> cells;
 cells = new List<ViewCell>();

InAppProductsListView.ItemsSource = cells;

我们应该将我们的特殊数据列表分配给listView的ItemsSource

您可以参考下面的示例代码:

      public ListPageCS()
    {
        Title = "BindingContextChanged Code Demo";
        Padding = 10;

        var customCell = new DataTemplate(typeof(CustomCell));
        customCell.SetBinding(CustomCell.NameProperty, "Name");
        customCell.SetBinding(CustomCell.AgeProperty, "Age");
        customCell.SetBinding(CustomCell.LocationProperty, "Location");

        var listView = new ListView
        {
            ItemTemplate = customCell
        };

        var button = new Button { Text = "Change Binding Context" };
        button.Clicked += (sender, e) =>
        {
            listView.ItemsSource = Constants.People;
        };

        Content = new StackLayout
        {
            Children = {
                listView,
                button
            }
        };
    }

自定义ViewCell的使用方法,可以查看文档Customizing ListView Cell Appearance.

您可以查看上面包含的示例 link。

样本在这里:https://github.com/xamarin/xamarin-forms-samples/tree/main/UserInterface/ListView/BindingContextChanged .