Xamarin forms global storage

ぐ巨炮叔叔 提交于 2021-02-05 07:48:32

问题


I am new to xamarin forms. I am working on an app with 3 pages. One of the pages has a listview. Clicking on a row takes you to another page. I want to make this available for authenticated users only. You have to be logged in to use this feature.

I have been looking online and no luck. Is there anything like session in xamarin forms? Is there anything I can use for storage which all pages can have access to?

Any ideas/link will be appreciated


回答1:


As Jason mentions, use Application.Current.Properties dictionary to store key pair values.

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/application-class/#Properties_Dictionary

Fairly simple and doesn't require an additional dependency like Xam.Plugins.Settings.




回答2:


There's a cross platform settings plugin available from the Xamarin Component Store or as a nuget package. It's free and easy to use.

https://www.nuget.org/packages/Xam.Plugins.Settings/

It's as easy as creating a "Settings" class and then creating a getter and setter for your value like this:

public static bool Authenticated
{
   get { return AppSettings.GetValueOrDefault<bool>(AuthenticatedKey, AuthenticatedDefaultValue); }
   set { AppSettings.AddOrUpdateValue<bool>(AuthenticatedKey, value); }
}

It's well worth a look and super easy to implement for simple adhoc storage of values.



来源:https://stackoverflow.com/questions/45964321/xamarin-forms-global-storage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!