这是展示问题的基本示例。
我有一个MobileServiceClient和离线SyncTable
private MobileServiceClient client = new MobileServiceClient("http://localhost:19087/");
private IMobileServiceSyncTable<TodoItem> todoTable;
我在客户端上有一个TodDoItem.cs
public class TodoItem
{
public string Id { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public string Text { get; set; }
public bool Complete { get; set; }
}
我初始化我的离线数据库就像这样
async Task Initialise()
{
if (!client.SyncContext.IsInitialized)
{
var store = new MobileServiceSQLiteStore("todo.db");
store.DefineTable<TodoItem>();
await client.SyncContext.InitializeAsync(store, new MobileServiceSyncHandler());
}
todoTable = client.GetSyncTable<TodoItem>();
}
并在线离线同步在线数据
async Task RefreshData()
{
await todoTable.PullAsync("todo", null);
var result = await todoTable.ToCollectionAsync();
listItems.ItemsSource = result;
}
现在问题是CreatedAt属性始终为null。如果我使该属性不可为空,那么它显示为日期01/01/0001(基本上是DateTime.Min)。 DB中的记录具有正确的日期。使用fiddler进行测试显示正确的日期正在从MobileService
序列化如果我改为在线环境,即
private IMobileServiceTable<TodoItem> todoTable;
然后一切都按预期工作。 CreatedAt显示正确的日期。 我已经尝试了Carlos' fix here所示的JsonConverter,但由于日期已经是01/01/0001(错误),因为它进入转换器时不起作用。 我真的需要按日期订购记录,并且无法弄清楚为什么它不能在离线环境中工作。任何帮助表示赞赏。
答案 0 :(得分:2)
默认情况下,系统属性(createdAt,version,updatedAt等)不会在查询中下拉。像这样更新你的定义会让它从服务器请求列,并知道将它放入什么值。
public class TodoItem
{
public string Id { get; set; }
[CreatedAt]
public DateTimeOffset? CreatedAt { get; set; }
[UpdatedAt]
public DateTimeOffset? UpdatedAt { get; set; }
public string Text { get; set; }
public bool Complete { get; set; }
}