这是一个显示问题的基本示例。
我有一个 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)。数据库中的记录具有正确的日期。使用 fiddler 进行测试显示正确的日期正在从 MobileService 中正确序列化
如果我更改为在线环境即
private IMobileServiceTable<TodoItem> todoTable;
然后一切都会按预期进行。 CreatedAt 显示正确的日期。 我尝试过 JsonConverter,如 Carlos' fix here 所示但这不起作用,因为日期进入转换器时已经是 01/01/0001 (错误)。 我确实需要按日期订购记录,但无法弄清楚为什么这在离线环境中不起作用。任何帮助表示赞赏。
最佳答案
默认情况下,系统属性(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; }
}
关于c# - 使用 Azure 移动服务中的脱机表时 CreatedAt 属性为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150956/