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("..");
}
}
CodePudding user response:
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();