xamarin 从AccountStore迁移到SecureStorage在Android设备上不起作用

d4so4syb  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(145)

我正在尝试将应用设置从使用AccountStore的旧Xamarin应用迁移到使用SecureStorage的新Xamarin Forms应用。我在StackOverflow和其他网站上找到了一些文章,展示了如何做到这一点,但是建议从AccountStore中检索旧值的方法对我不起作用。
在我以前的Android项目中,我有这样的代码。在我测试过的模拟器中,account变量的值是正确的。我使用的是Xamarin.Auth.Androidv1.2.3.1,如果这很重要的话。

var account = AccountStore.Create(Android.App.Application.Context).FindAccountsForService("x.y.z").FirstOrDefault();

在我的Xamarin Forms项目中,我有以下代码,可以在iOS设备和Android 6.0,8.0和10.0模拟器上工作,但不能在Android 6.0或8.0设备上工作,这是我现在唯一可以访问的设备。

var account = AccountStore.Create().FindAccountsForService("x.y.z")?.FirstOrDefault();

我还在我的新Xamarin Forms应用程序的Android项目的MainActivity的OnCreate中尝试了这一点。

var accounts = AccountStore.Create(this).FindAccountsForService("x.y.z");
var accounts = AccountStore.Create(Android.App.Application.Context).FindAccountsForService("x.y.z");

如果我从Google Play商店下载旧的Xamarin应用程序到我的设备上,输入我的设置,然后将其替换为从Visual Studio部署的新Xamarin Forms应用程序,我不会获得任何设置。但是,如果我将旧的Xamarin应用程序从Visual Studio部署到我的设备上,输入我的设置,然后将其替换为从Visual Studio部署的新Xamarin Forms应用程序,我确实会获得我的设置。是否可以安全地假设,一旦我在Google Play商店上安装了新应用程序,一切都将正常工作?

d6kp6zgx

d6kp6zgx1#

你看过github wiki上关于Migrating from AccountStore to Xamarin.Essentials SecureStorage的官方文档了吗?
您可以尝试使用var accountStoreAccounts = AccountStore.Create().FindAccountsForService(""x.y.z"");来检查是否可以获得正确的值。
然后使用Migration / Helper类将保存的帐户迁移到SecureStorage。之后,您可以检索存储的帐户。

相关问题