如何更新WalletItem

本文关键字:WalletItem 更新 何更新 | 更新日期: 2023-09-27 18:05:30

如何更新WalletItem?我可以删除然后添加WalletItem,但当我这样做时,WalletItem的磁贴会从开始屏幕上消失,我认为还会发生其他不好的事情。

如何在不删除WalletItem的情况下更新它?

如何更新WalletItem

使用存储的UpdateAsync方法。

这之所以有效,是因为当你通过WalletItemsStore.GetItemssync((获得票证时,WalletItems已经设置了Id。这是只读属性。因此,当您通过WalletItemStore.UpdateAsync((方法将该WalletItem发回时,Store会从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"
        });
}