【发布时间】:2021-07-21 13:27:54
【问题描述】:
我有一个似乎没有人能够弄清楚的神秘问题。我正在使用 MVVM 模式构建一个带有 Xamarin 和类库的应用程序。
当我尝试绑定标签时,我有一个连接到列表的列表视图,但不会连接到属性。我已经尝试了一切,一点一点地删除了所有代码,看看是否改变了一些东西,但没有任何效果。 我已经解决这个问题好几天了,如果有人可以帮助我,我将非常感激。
我已经检查并且列表成功进入 UI,我怀疑错误在 xaml 代码中,但如果可能是其他代码,我会发布大部分代码。
页面:
<StackLayout>
<!-- Searchbar -->
<StackLayout>
<SearchBar
TextColor="black"
FontSize="24"
PlaceholderColor="Black"
Placeholder="Sök lista..."
Margin="0,5,30,10"
SearchCommand="{Binding PerformSearch}"/>
</StackLayout>
<!-- Listview of inventory lists -->
<ListView BackgroundColor="#7EA0B7"
SeparatorColor="#7EA0B7"
x:Name="listView"
ItemsSource="{Binding ListOfLists}"
HasUnevenRows="True"
SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5">
<Frame CornerRadius="10" BackgroundColor="white">
<StackLayout>
<Label
TextColor="Black"
FontFamily="PTC55F.ttf#ptc55f"
VerticalOptions="Center"
Text="{Binding ListName}" <----- can't debug cause it can't find it
FontSize="Large"/>
<Label
FontFamily="PTC55F.ttf#ptc55f"
Margin="0,5,0,5"
Text="{Binding DateSent}"<----- can't debug cause it can't find it FontSize="Medium"/>
<Label
FontFamily="PTC55F.ttf#ptc55f"
Margin="0,5,0,5"
Text="{Binding Date}" <----- can't debug cause it can't find it
FontSize="Medium"/>
</StackLayout>
</Frame>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- Create new list button -->
<StackLayout>
<Button Text="+ Skapa ny lista"
Margin="8"
HeightRequest="60"
FontSize="24"
FontFamily="PTC55F.ttf#ptc55f"
BackgroundColor="#AAC0AA"
TextColor="black"
BorderColor="Black"
BorderWidth="2"
Command="{Binding NavigateCreateListCommand}"/>
</StackLayout>
</StackLayout>
</ContentPage>
xaml.cs
public partial class InventoryStartPage : ContentPage
{
public InventoryStartPage()
{
InitializeComponent();
BindingContext = new InventoryStartViewModel(new ListManager(new MockListRepo());
}
虚拟机
public ObservableCollection<ListModel> ListOfLists { get; set; }
private ListManager _manager;
public InventoryStartViewModel(ListManager manager, NavigationServices navigationService)
{
_manager = manager;
ListOfLists = _manager.GetLists();
}
经理
public class ListManager
{
public ObservableCollection<ListModel> ListList = new ObservableCollection<ListModel>();
private readonly IMockListRepo _mockRepo;
public ListManager(IMockListRepo mockRepo)
{
_mockRepo = mockRepo;
}
public ObservableCollection<ListModel> GetLists()
{
foreach (var list in _mockRepo.GetAllLists())
{
ListList.Add(list);
}
return ListList;
}
}
【问题讨论】:
-
你的 ListModel 是什么样子的?
-
public class ListModel { public int ListId { get; set; } public string ListName { get; set; } public DateTime DateCreated { get; set; } public DateTime? DateSent { get; set; } } -
@awkward_coder 添加断点检查 ListOfLists 是否有数据?我测试了你的代码,没有问题。
-
我有,所以它不起作用仍然很奇怪。当我将列表视图连接到从一开始就在视图模型中模拟的列表时,绑定就起作用了。
-
尝试更改您的标签之一以使用硬编码的文本值 - 这将验证 ListView 有数据并且模板布局正在工作
标签: listview xamarin mvvm binding