如何更新WalletItem?我可以删除然后添加WalletItem
但是当我这样做时,WalletItem
的图块会从开始屏幕消失,我认为其他不好的事情也会发生。
如何更新WalletItem
而不删除它?
答案 0 :(得分:1)
使用商店的UpdateAsync方法。
为什么这样做有用的秘诀是当你通过WalletItemStore.GetItemsAsync()获得一张票时,WalletItems已经设置了Id
。这是一个只读属性。因此,当您通过WalletItemStore.UpdateAsync()方法发回WalletItems时,商店会从WalletItem中读取ID。
这对我来说有点奇怪,至少因为WalletItemStore.AddAsync()接受Id作为参数而不是作为项目的属性。
示例:
var newTicket = MyService.GetTicket();
var store = await WalletManager.RequestStoreAsync();
var currentTickets = await store.GetItemsAsync();
if (currentTickets.Count(x => x.Id == newTicket.SerialNumber) == 1)
{
// the ticket exist so let's update it
// get the "old" ticket from the Wallet store
var ticket = currentItems.First(x => x.Id == newTicket.SerialNumber);
// update the "old" ticket with the updated information
ticket.DisplayName = newTicket.Name;
ticket.BodyColor = Color.FromArgb(255, 255, 0, 0);
// put the updated "old" ticket back in the store again
await store.UpdateAsync(ticket);
}
else
{
// the ticket does not exist so let's add it
await store.AddAsync(newTicket.SerialNumber, new WalletItem(WalletItemKind.Ticket, newTicket.Name)
{
BodyFontColor = Color.FromArgb(255, 255, 255, 255),
BodyColor = Color.FromArgb(255, 50, 50, 230),
DisplayName = newTicket.Name,
ExpirationDate = newTicket.ExpireDate,
IssuerDisplayName = "My Company"
});
}