与使用属性字典相比,将应用程序设置存储为App()静态变量是否存在任何性能问题?

时间:2018-09-30 20:40:21

标签: xamarin xamarin.forms

我当前正在存储应用程序特定的变量,如下所示:

public partial class App : Application
{
   public static int id;

当我的应用程序启动时,我从数据库中的表中加载这些值,并且当它们更改时,我更改了值并更新了数据库。

在这里,我知道有另一种方式可以做到这一点:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/application-class#Properties_Dictionary

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数据库中更有效吗?

1 个答案:

答案 0 :(得分:2)

Properties字典被保存到设备中,而static类中的App变量未持久化(应用程序运行时它们存在于内存中)。

因此,这取决于您是否要在生命周期状态变化中保留信息,Properties字典是您的理想选择。