我当前正在存储应用程序特定的变量,如下所示:
public partial class App : Application
{
public static int id;
当我的应用程序启动时,我从数据库中的表中加载这些值,并且当它们更改时,我更改了值并更新了数据库。
在这里,我知道有另一种方式可以做到这一点:
if (Application.Current.Properties.ContainsKey("id"))
{
var id = Application.Current.Properties["id"] as int;
// do something with id
}
但是当我尝试使用与Xamarin示例页面中的代码相同的代码进行编码时,会给我一个错误:
App.xaml.cs(26,26): Error CS0077: The as operator must be used with a reference type or nullable type ('int' is a non-nullable value type) (CS0077)
有人可以告诉我如何解决此错误,并且使用属性字典比将信息存储在SQLLite数据库中更有效吗?
答案 0 :(得分:2)
Properties
字典被保存到设备中,而static
类中的App
变量未持久化(应用程序运行时它们存在于内存中)。
因此,这取决于您是否要在生命周期状态变化中保留信息,Properties
字典是您的理想选择。