sqlite:显示标签中列值的总和
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是设置私有字段
buffictMoney
,该不调用property Changed
,您应该设置 public property ,它将调用
property> property> property changed
this is setting the private field
budgetMoney
, which does NOT callPropertyChanged
instead, you should set the public property, which will call
PropertyChanged