SQLite:显示标签中列值的总和

SQLite: Show sum of values of a column in the label

我是 Xamarin 的新手 - 我遇到了一个问题。如何在 SQLite 的标签中显示列值的总和?

这是我的代码。

车型预算

 public class Budget
 {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public int Money { get; set; }
 }

SQLite 数据库 + 方法 GetBudgets()

public class StatisticsService
{
    static SQLiteAsyncConnection db;
    static async Task Init()
    {
        if (db != null)
            return;
        // Get an absolute path to the database file
        var databasePath = Path.Combine(FileSystem.AppDataDirectory, "MyApp.db");

        db = new SQLiteAsyncConnection(databasePath);
        await db.CreateTableAsync<Budget>();
    }

    public static async Task AddBudget(int money)
    {
        await Init();
        var budget = new Budget
        {
            Money = money,
        };

        await db.InsertAsync(budget);
    }

    public static async Task<int> GetBudgets()
    {
        await Init();

        int sumBudgets = await db.ExecuteScalarAsync<int>("SELECT SUM(Money) FROM Budget");
        return sumBudgets;
    }

}

ViewModel代码

    int budgetMoney;
    public int  BudgetMoney { get => budgetMoney; set => SetProperty(ref budgetMoney, value); }
    public AsyncCommand OpenAddBudget { get; }
    public AsyncCommand ListBudget { get; }

    public StatisticsViewModel()
    {

        OpenAddBudget = new AsyncCommand(Open);
        ListBudget = new AsyncCommand(ListGetBudget);

        
    }
    async Task Open()
    {
        var route = "addBudgetPage";
        await Shell.Current.GoToAsync(route);
    }
    async Task ListGetBudget()
    {
        budgetMoney = await StatisticsService.GetBudgets();
    }

查看Xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="MyApp.Views.Statistics"
         xmlns:viewmodels="clr-namespace:MyApp.ViewModels"
         xmlns:model="clr-namespace:MyApp.Models">
<ContentPage.BindingContext>
    <viewmodels:StatisticsViewModel/>
</ContentPage.BindingContext>
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Add" Command="{Binding OpenAddBudget}"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
    <StackLayout>
        <Button Text="Reload" Command="{Binding ListBudget}"/>
        <Label Text="{Binding BudgetMoney}"/>
    </StackLayout>
</ContentPage.Content>

我没有收到任何错误,但在调试时我注意到变量 sumBudget 始终为 0。我的 SQLite 语法有问题吗?

public static async Task<int> GetBudgets()
    {
        await Init();

        int sumBudgets = await db.ExecuteScalarAsync<int>("SELECT SUM(Money) FROM Budget");
        return sumBudgets;
    }

不幸的是,我不知何故没有更进一步。目标应该是当我单击“重新加载”按钮时,各个预算的总和显示在标签中。

感谢您的帮助!

编辑: 调用添加按钮

public class AddBudgetViewModel : ViewModelBase
{
    int money;
    public int Money { get => money; set => SetProperty(ref money, value); }
    public AsyncCommand SaveCommand { get; }

    public AddBudgetViewModel()
    {
        SaveCommand = new AsyncCommand(Save);

    }
    async Task Save()
    {
        if (money == 0)
            return;

        await StatisticsService.AddBudget(money);

        await Shell.Current.GoToAsync("..");
    }
}

这是设置私有字段 budgetMoney,它不会调用 PropertyChanged

budgetMoney = await StatisticsService.GetBudgets();

相反,您应该设置 public 属性,这将调用 PropertyChanged

BudgetMoney = await StatisticsService.GetBudgets();