gpt4 book ai didi

c# - NSUserDefaults-Xamarin

转载 作者:可可西里 更新时间:2023-11-01 08:26:59 24 4
gpt4 key购买 nike

我只是想保存/恢复几个 double 。我所看到的是它在连接到调试器时工作,但在应用程序关闭 15 分钟后 - 重新启动应用程序并为两个 double 恢复“-180”。

这看起来对吗?或者你认为我以某种方式节省了“-180”?

这是设备而不是模拟器。我正在使用 Map.DidUpdateUserLocation,它给出了 MKUserLocationEventArgs

它以某种方式给出了 -180,-180 的位置。可能是 Xamarin 错误?

    private CLLocationCoordinate2D GetLastLocation()
{
var user = NSUserDefaults.StandardUserDefaults;

double lat = user.DoubleForKey ("LastPositionLat");
double lng = user.DoubleForKey ("LastPositionLng");

var location = new CLLocationCoordinate2D (lat, lng);
return location;
}

private void SaveLastLocation(CLLocationCoordinate2D coord)
{
var user = NSUserDefaults.StandardUserDefaults;
user.SetDouble (coord.Latitude, "LastPositionLat");
user.SetDouble (coord.Longitude, "LastPositionLng");
user.Synchronize ();
}

最佳答案

这个问题很老了,但它让我突然想到了一个原因,我们看到非常奇怪的行为将 NSUserDefaults.StandardUserdefaults 存储在局部变量中,然后重复访问它。

Xamarin.iOS 绑定(bind)中的某些内容(至少对于 MonoTouch,即非统一的 API)在那里很糟糕,可能是对象失去其 native 对等体的 GC 问题。我们的代码库中有这个:

    // it appears that capturing an instance of NSUserDefaults.StandardUserdefaults in a variable does not work in every case
// we have observed a voodoo bug when reading a written value from such an instance would fail (as I said, voodoo!)
// thus, we directly access standard user defaults here always via the lambda

关于c# - NSUserDefaults-Xamarin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18091075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com