【问题标题】:Xamarin.Forms application properties not removing keysXamarin.Forms 应用程序属性不删除键
【发布时间】:2021-09-17 23:42:13
【问题描述】:

在一个页面中,我从我的应用程序属性中删除了一个键:

async void SaveAndClose()
{
    Application.Current.Properties.Remove(FOO_KEY);
    await Application.Current.SavePropertiesAsync();    

    foreach (var key in Application.Current.Properties.Keys;)
    {
        Debug.WriteLine($"Key in properties: {key}"); //FOO_KEY is missing, good!
    }
    await Navigation.PopModalAsync();
}

似乎一切正常。钥匙不见了,只剩下其他东西了。但是,如果我将以下内容放在出现在Navigation.PopModalAsync 之后的页面中:

protected override async void OnAppearing()
{
    base.OnAppearing();
    foreach (var key in Application.Current.Properties.Keys;)
    {
        Debug.WriteLine($"Key in properties: {key}"); //FOO_KEY is back!
    }
}

突然间,键和它的值都返回了!是什么导致字典不保存自己?由于文档显示 Application.Properties 是“只是”一个 IDictionary,因此没有列出任何陷阱。怎么回事?

我正在使用 Xamarin 4.12.3.83。

【问题讨论】:

  • 在 iOS 上吗?您是否尝试过改用 Xamarin Essentials 首选项?
  • @Prateek:目前我只在安卓上测试过。

标签: c# xamarin xamarin.forms


【解决方案1】:

我建议你使用Xamarin Essentials Preferences

只有在您卸载应用程序时才会删除应用程序属性

或者如果您仍然想删除,您可能必须在 android 上将 autobackup 设置为 false 并且不将 iPhone 链接到 iCloud。对于首选项,您还需要将其设置为 false 才能将其永久删除。

Reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 2023-03-03
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多