Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime in ApplicationData.LocalSettings

Tags:

c#

uwp

From this blog post, it seems I should be able to store a DateTime as a ApplicationData.LocalSettings value, but I'm getting the exception below for this line.

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

What am I missing?

I'm currently converting the DateTime to string to make this work.

like image 897
Carlo Mendoza Avatar asked Nov 20 '16 23:11

Carlo Mendoza


1 Answers

I think it's a link redirection issue of that Blog which causes your misunderstanding of supported data type DateTime. You can refer to the official document: Types of app data, the DateTime in this document will link you to 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.

It means, the supported type for date here is System.DateTimeOffset Structure, not System.DateTime Structure. So, you can either modify your code like this:

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

Or you can keep using your ToString() method to save it, but when you retrieve this string type value, you may need to rebuild the string for using it in your code.

like image 58
Grace Feng Avatar answered Oct 22 '22 21:10

Grace Feng