从这篇博文看来,我应该能够将 DateTime
存储为 ApplicationData.LocalSettings
值,但我在下面得到了这一行的异常.
ApplicationData.Current.LocalSettings.Values["LastTokenRefresh"] = DateTime.UtcNow;
异常(exception):
Data of this type is not supported.
Error trying to serialize the value to be written to the application data store
我错过了什么?
我目前正在将 DateTime
转换为字符串以使其工作。
最佳答案
我认为是该博客的链接重定向问题导致您对支持的数据类型 DateTime
产生误解。可以引用官方文档:Types of app data ,本文档中的 DateTime
会将您链接到 DateTime structure :
JavaScript: This type appears as the Date object.
.NET: When programming with .NET, this type is hidden, and developers should use the System.DateTimeOffset structure.
C++: Similar to FILETIME but with important differences.
也就是说,这里支持的日期类型是System.DateTimeOffset Structure , 不是 System.DateTime Structure .因此,您可以像这样修改您的代码:
ApplicationData.Current.LocalSettings.Values["LastTokenRefresh"] = DateTimeOffset.UtcNow;
或者您可以继续使用您的 ToString()
方法来保存它,但是当您检索此字符串类型值时,您可能需要重建字符串以便在您的代码中使用它。
关于c# - ApplicationData.LocalSettings 中的日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40710715/