我試圖讓影像顯示在我的自定義 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;
}
}
以下是自定義 viewcell 的 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>
任何幫助將不勝感激,因為這個超級簡單的事情讓我發瘋:/
uj5u.com熱心網友回復:
我認為你使用它的方式有問題。
例如:
1.您沒有為您的ViewCell.
2.為什么分配cells給InAppProductsListView.ItemsSourcewhile其子元素的型別是ViewCell?
private List<ViewCell> cells;
cells = new List<ViewCell>();
InAppProductsListView.ItemsSource = cells;
我們應該將我們的特殊資料串列分配給ItemsSourcelistView。
您可以參考以下示例代碼:
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 ,您可以查看檔案自定義 ListView Cell Appearance。
您可以查看上面鏈接中包含的示例。
示例在這里:https ://github.com/xamarin/xamarin-forms-samples/tree/main/UserInterface/ListView/BindingContextChanged 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480844.html
