【问题标题】:Failing to bind items in MAUIMAUI绑定物品失败
【发布时间】:2023-01-11 01:30:47
【问题描述】:

//My Model

public class BookInfo
    {
        public string BookName { get; set; }
        public string BookDescription { get; set; }
    }

//my View Model

 public ObservableCollection<BookInfo> Bookmodel { get; set; }

        public BookRepoInfo()
        {
            Bookmodel = new ObservableCollection<BookInfo> { //**is this correct way.**
                new BookInfo { BookName = "Object-Oriented Programming in C#", BookDescription = "Object-oriented programming is a programming paradigm based on the concept of objects" },
             ......
            };
        }

XAML 页面:

 <ContentPage.BindingContext>
   <local:BookRepoInfo />
 </ContentPage.BindingContext>
 <X:YList ItemsSource="{Binding Bookmodel}"></X:YList>

使用 MVVM 模式加载列表项

【问题讨论】:

  • 无论 YList 是什么,您都需要提供一个模板来定义每一行的显示方式

标签: mvvm maui


【解决方案1】:

假设 YList 是对 ListViewCollectionView 的继承,您需要提供某种模板,您希望将其应用于该列表的每个单元格。

现在正在发生的事情是它只会调用你放入的对象上的ToString()

将您的代码更改为:

<X:YList ItemsSource="{Binding Bookmodel}">
    <X:YList.ItemTemplate>
        <DataTemplate>
            <VerticalStackLayout>
                <Label Text="{Binding BookName}"/>
                <Label Text="{Binding BookDescription}"/>
            </VerticalStackLayout>
        </DataTemplate>
    </X:YList.ItemTemplate>
</X:YList>

更多信息在这里:https://learn.microsoft.com/dotnet/maui/user-interface/controls/collectionview/populate-data?view=net-maui-7.0#define-item-appearance

【讨论】:

    猜你喜欢
    • 2023-02-14
    • 2012-01-14
    • 2012-02-25
    • 2015-02-02
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    相关资源
    最近更新 更多