简体   繁体   English

我需要将表数据放入ListView(MVVM)

[英]I need to put my table data into ListView (MVVM)

Database connection and binding context are okay. 数据库连接和绑定上下文都可以。 Data can insert into a table without a problem. 数据可以毫无问题地插入表中。 I need to add last of my table data row to a list view 我需要将表数据行的最后一个添加到列表视图

<ListView ItemsSource="{Binding YourList}" 
          verticalOptions="FillAndExpand" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding Catagoryview}"/>
                    <Label Text="{Binding Date}"/>
                    <Label Text="{Binding Expense}"/>

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

this is my XAML file. 这是我的XAML文件。 I create every property for in binding here. 我在这里创建用于绑定的每个属性。 with backing fields. 与支持领域。

in my ViewModel 在我的ViewModel中

private void add()
{
    database = new Database();
    var expensedata = database.GetExpenses(id);

    if (expensedata != null)
    {
        Catagoryview = expensedata.Catagory;
        Date= expensedata.Date;
        Expense = expensedata.Expense; 
    }
}

I know this code is not perfect. 我知道这段代码并不完美。 I really need to get the last 10 rows of my data into a list view. 我确实需要将数据的最后10行放入列表视图。

also, here my get expenses method I used 另外,这是我以前使用的支出方式

public AddExpenses GetExpenses(int expense)
{
    return Conn.Table<AddExpenses>().FirstOrDefault(t => t.Id == expense);
}

To me it seems as if your add method does not actually add anything. 对我来说,似乎您的add方法实际上并未添加任何内容。 In your viewmodel you'll need to have a public property YourList , which is propagated with add . 在您的视图模型中,您需要具有一个公共属性YourList ,该属性与add传播。 Furthermore you are getting only one item from your expenses database, which won't work for adding 10 items. 此外,您从费用数据库中仅获得一项,这对于添加10项无效。

The core question as far as I understood is 据我了解,核心问题是

How can I add the 10 last items from an SQLite database to a Xamarin.Forms ListView using an MVVM approach. 如何使用MVVM方法将SQLite数据库中的最后10个项目添加到Xamarin.Forms ListView

Disregarding the fact that I do not know how and when you are calling add , I'll try to give an answer on how to get and add these elements. 不考虑我不知道如何以及何时调用add的事实,我将尝试给出有关如何获取和添加这些元素的答案。

First of all we need a property that will hold your items in your viewmodel. 首先,我们需要一个可将您的项目保存在视图模型中的属性。 Depending on your requirements, either an IEnumerable<AddExpenses> or an ObservableCollection<AddExpenses> will do. 根据您的要求,可以使用IEnumerable<AddExpenses>ObservableCollection<AddExpenses> I will demonstrate the ObservableCollection approach 我将演示ObservableCollection方法

ObservableCollection<AddExpenses> _expenses;

public ObservableCollection<AddExpenses> Expenses
{
    get
    {
        if(_expenses == null)
        {
            _expenses = new ObserrvableCollection<AddExpenses>();
        }
        return _expenses;
    }
}

You can bind to that property from your XAML 您可以从XAML绑定到该属性

<ListView ItemsSource="{Binding Expenses}" 
      VerticalOptions="FillAndExpand">
<!-- What ever -->
</ListView>

And then add items to that collection from your AddExpenses method (I felt free to give it a more meaningful name): 然后从您的AddExpenses方法向该集合中添加项目(我可以随意给它起一个更有意义的名称):

private void AddExpenses()
{
    var database = new Database();
    var expenses = database.GetLastExpenses(numberOfExpenses: 10);

    foreach(var expense in expenses)
    {
        Expenses.Add(expense);
    }
}

Since we are now having objects of type AddExpenses in your collection, we'll have to change the binding in the ListView from Categoryview to Category : 由于我们现在在您的集合中具有AddExpenses类型的对象,因此我们必须将ListView的绑定从Categoryview更改为Category

<ListView ItemsSource="{Binding YourList}" 
          VerticalOptions="FillAndExpand">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding Category}"/>
                    <Label Text="{Binding Date}"/>
                    <Label Text="{Binding Expense}"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Last but not least we'll have to implement GetLastExpenses (this should work, while it's possible that there are better options) 最后但并非最不重要的一点是,我们必须实现GetLastExpenses (这应该可以工作,虽然可能有更好的选择)

public AddExpenses[] GetLastExpenses(int numberOfExpenses)
{
    return Conn.Table<AddExpenses>()
               .OrderByDescending(expenses => expenses.Date) 
               .Take(numberOfExpenses)
               .OrderBy(expenses => expenses.Date) // If you need them ordered ascending
               .ToArray()

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM