c# - ApplicationData.LocalSettings 中的日期时间

标签 c# uwp

从这篇博文看来,我应该能够将 DateTime 存储为 ApplicationData.LocalSettings 值,但我在下面得到了这一行的异常.

ApplicationData.Current.LocalSettings.Values["LastTokenRefresh"] = DateTime.UtcNow;

https://blogs.windows.com/buildingapps/2016/05/10/getting-started-storing-app-data-locally/#bgpwEqDbEt0GClHB.97

异常(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/

相关文章:

c# - 在文本框中显示变量

c# - 在 Entity Framework 6 中投影自引用多级实体

c# - 更改 .asp 文件样式 (css)

c# - 如何在 C# 中解析可为空的 DateTime 对象

c# - GoF Factory的命名约定?

visual-studio - 创作者更新目标版本UWP

c# - 如何在枚举中获取枚举值的 int

c# - NavigationView 和其他设置按钮

c# - 如何在 UWP App 中创建信息提示通知

c# - 从 UWP BackgroundTask 调用 MediaCapture.InitializeAsync