简体   繁体   English

如何为移动应用制作本地存储存储文件

[英]How can I make a local storage storage file for mobile app

I'm building a Xamarin CrossPlatform app all of its database is in cloud api's, but I want a local storage file in mobile which stores all login credentials whenever someone is login.我正在构建一个 Xamarin CrossPlatform 应用程序,它的所有数据库都在云 API 中,但我想要一个本地存储文件,用于在有人登录时存储所有登录凭据。

Kindly help me through this!请帮助我解决这个问题!

If you want to just save the login credentials you should use Xamarin.Essentials Storage如果只想保存登录凭据,则应使用Xamarin.Essentials Storage

First, Add the Xamarin.Essentials NuGet package to each project.首先,将 Xamarin.Essentials NuGet 包添加到每个项目。

In Android在安卓中

protected override void OnCreate(Bundle savedInstanceState) {
    //...
    base.OnCreate(savedInstanceState);
    Xamarin.Essentials.Platform.Init(this, savedInstanceState); // add this line to your code
    //...

To handle runtime permissions on Android, Xamarin.Essentials must receive any OnRequestPermissionsResult.若要在 Android 上处理运行时权限,Xamarin.Essentials 必须接收任何 OnRequestPermissionsResult。 Add the following code to all Activity classes:将以下代码添加到所有 Activity 类:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
    Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

And then to save:然后保存:

try
{
  await SecureStorage.SetAsync("loginCredential", "loginCredentialValue");
}
catch (Exception ex)
{
  // Possible that device doesn't support secure storage on device.
}

To retrieve:检索:

try
{
  var loginCredential = await SecureStorage.GetAsync("loginCredential");
}
catch (Exception ex)
{
  // Possible that device doesn't support secure storage on device.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从本地存储中的文件而不是 res/raw 中的硬编码文件中进行选择? - How can I choose from files in local storage instead of from a hardcoded file in res/raw? 从服务器下载.xml文件并保存到Windows Mobile 6.5 .net Compact Framework中的本地存储中 - download .xml file from server and save to local storage in windows mobile 6.5 .net compact framework 如何在不存储中间文件的情况下使XML文档可下载? - How to make an XML document downloadable without intermediate file storage? 本地存储文件中的访问被拒绝错误 - Access denied Error in Local storage file 如何解析列存储格式的 XML 文件? - How do I parse XML file in columnar storage format? 如何在应用程序存储中存储多个值 - How can I store several values in Application storage 如何将共享首选项文件从内部存储传输到外部存储? - How to transfer sharedpreferences file from internal storage to external storage? 可以从隔离存储中读取xml文件 - can read xml file from isolated storage 如何将 output wav 文件从 tts 保存到内部存储,使用下面的代码我将它保存在外部存储中 - how to save a output wav file from tts to internal storage, using the below code I am to save it in external storage 如何在内部存储Android中保存XML文件 - How to save an XML file in Internal Storage Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM