我使用 FreshMvvm 在 Windows 上開發和運行 MAUI 專案。
但是我對 ListView 和我的自定義模板有一些系結問題。
以下是我的代碼:
模型:
public class BaseModel
{
public string Code{ get; set; }
}
public class NameModel: BaseModel
{
public string Name{ get; set; }
}
視圖模型:
public class MainPageModel : FreshBasePageModel
{
private readonly IApiService _apiService;
private List<NameModel> _nameModelList;
public List<NameModel> NameModelList
{
get => _nameModelList;
private set
{
_nameModelList= value;
RaisePropertyChanged(nameof(NameModelList));
}
}
public MainPageModel(IApiService apiService)
{
_apiService = apiService;
}
protected override void ViewIsAppearing(object sender, EventArgs e)
{
base.ViewIsAppearing(sender, e);
Task.Run(() => GetNameData());
}
private async Task GetNameData()
{
var result = await _apiService.GetNameData();
NameModelList= result.GetRange(1, 10);
}
}
我創建一個串列并使用 api 服務來獲取名稱模型串列資料。如果 api 服務獲取資料,NameModelList將被更新。
NameModelList 是將系結在 Listview.ItemsSource 上的屬性
主頁.xmal:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyNamespace.ViewCells.CustomListViewCell"
x:Class="MyNamespace.Pages.MainPage"
BackgroundColor="{DynamicResource SecondaryColor}">
<Grid RowSpacing="25"
RowDefinitions="Auto"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<ListView
x:Name="MyListView"
ItemsSource="{Binding NameModelList}"
Grid.Row="0"
WidthRequest="800"
HeightRequest="800"
BackgroundColor="Gray"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<local:MyCustomViewCell/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</ContentPage>
自定義 ViewCell (.xml):
<ViewCell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyNamespace.ViewCells.CustomListViewCell.MyCustomViewCell">
<Grid RowSpacing="100" WidthRequest="100" HeightRequest="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" />
</Grid.ColumnDefinitions>
<StackLayout
GridLayout.Row="0"
GridLayout.Column="0">
<Label
Text="{Binding Code}"
FontSize="30"/>
<Label
Text="{Binding Name}"
FontSize="30"/>
</StackLayout>
</Grid>
</ViewCell>
自定義 ViewCell (.cs)
public partial class MyCustomViewCell: ViewCell
{
public static readonly BindableProperty CodeProperty =
BindableProperty.Create("Code", typeof(string), typeof(MyCustomViewCell), "");
public string Code
{
get { return (string)GetValue(CodeProperty); }
set { SetValue(CodeProperty, value); }
}
public static readonly BindableProperty NameProperty =
BindableProperty.Create("Name", typeof(string), typeof(MyCustomViewCell), "");
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
}
我定義了一個自定義 ViewCell 檔案并將這個 ViewCell 放在 MainPage 的 Listview 中。
現在我的問題是我的 Listview 無法成功顯示資料。
我確信它NameModelList具有價值并且它的計數大于 1。
但我什么也看不見。
輸出日志沒有錯誤,MyCustomViewCell.cs永遠不會觸發里面的斷點。
所以我認為我有一些系結問題,但我找不到。
uj5u.com熱心網友回復:
為了深入了解這一點,我把你的代碼放在一個專案中,這樣我就可以玩一下它了。你可以在這里找到回購。不要在這里粗魯或任何事情,但對于下一個問題自己做這件事可能是個好主意,這將有助于加快速度:)
無論如何,問題要微妙得多。因為您使用 XAML 進行布局,所以您必須呼叫InitializeComponent建構式。因此,將其添加到您的MyCustomViewCell作業中:
public MyCustomViewCell()
{
InitializeComponent();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/406829.html
標籤:
