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

发布于 2025-01-28 02:54:28 字数 3421 浏览 1 评论 0原文

我是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;
    }

不幸的是,我以某种方式没有进一步。目标应该是,当我单击“重新加载”按钮时,标签中显示单个预算的总和。

感谢您的帮助!

编辑: 致电AddButton

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("..");
    }
}

I am new to Xamarin - and I'm encountering a problem. How can I display the sum of the values of a column in a label from SQLite?

Here is my code.

Model Budget

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

SQLite Database + Method 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 Code

    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();
    }

View 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>

I don't get any errors, but when debugging I noticed that the variable sumBudget is always 0. Do I have something wrong in the syntax of SQLite?

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

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

Unfortunately, I somehow do not get further. The goal should be that when I click on the button "Reload" the sum of the individual budgets are displayed in the label.

Thanks for your help!

Edit:
Call AddButton

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("..");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2025-02-04 02:54:28

这是设置私有字段buffictMoney,该不调用property Changed

budgetMoney = await StatisticsService.GetBudgets();

,您应该设置 public property ,它将调用property> property> property changed

BudgetMoney = await StatisticsService.GetBudgets();

this is setting the private field budgetMoney, which does NOT call PropertyChanged

budgetMoney = await StatisticsService.GetBudgets();

instead, you should set the public property, which will call PropertyChanged

BudgetMoney = await StatisticsService.GetBudgets();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文