0

I'm currently storing application specific variables like this:

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

When my application starts I load these values from a table in a database and when they change I change the value plus update the database.

Here I understand there's a different way to do this:

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
}

But when I try to code this using the same code as in the Xamarin example page it gives me an error:

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)

Can someone tell me how I can fix this error and also is it more efficient to use the properties dictionary vs storing the information in a SQLLite database?

4

1 回答 1

2

Properties字典保存到设备中,而您的类中的变量static没有保留App(它们在您的应用程序运行时存在于内存中)。

所以这取决于,如果你想在生命周期状态变化中持久化信息,Properties字典是你要走的路。

于 2018-09-30T21:03:28.813 回答