绑定在 Listview Xamarin MVVM 中不起作用

问题描述

我有一个似乎没有人能够弄清楚的神秘问题。我正在使用 Xamarin 和类库构建一个应用程序,使用 MVVM 模式。

当我尝试绑定标签时,我有一个连接到列表的列表视图,但不会连接到属性。我已经尝试了一切,一点一点地删除了所有的代码,看看是否改变了一些东西,但没有任何效果。 我已经解决这个问题好几天了,如果有人能帮助我,我将非常感激。

我已经检查过并且列表成功进入用户界面,我怀疑错误出在 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" 
                                Text="{Binding DateSent}"<----- can't debug cause it can't find it                                FontSize="Medium"/>

                                    <Label 
                                FontFamily="PTC55F.ttf#ptc55f"
                                Margin="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;

        }
    }

解决方法

终于!由于这个绑定问题,我把头发扯了几个小时。朋友们的解决方案来了!

        <ListView 
            BackgroundColor="White"
                  ItemsSource="{Binding ListOfArticles}"
            HasUnevenRows="True"
              SelectionMode="None">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="10" 
                         x:DataType="model:ArticleRegistrationModel"> <--------THISSSS
                            <Label Text="{Binding ArticleId}" 
                          
                            FontSize="16" />
                            <Label Text="{Binding ArticleName}" 
                            LineBreakMode="NoWrap"
                            FontSize="13" />
                            <Label Text="{Binding  }" 
                            LineBreakMode="NoWrap"
                            FontSize="13" />

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>


        </ListView>

将 x:DataType="model:ArticleRegistrationModel" 添加到围绕标签的 Stacklayout 最终修复了它。起初只出现了两个道具,但在构建之后它们都在那里。

希望这也能解决其他人的问题。