【发布时间】:2021-01-28 04:33:40
【问题描述】:
我正面临一个有趣的问题,我无法弄清楚可能是什么原因。
我正在尝试处理异步任务以检查我的应用是否有活动订阅。如果在 App Constructor 中完成,它就可以工作。但是放在Onstart()里面就不行了
这里;它正在按我的预期工作。它调用 BillingTasks.WasItemPurchasedAsync() 并等待它完成。
public App()
{
....
if (!Properties.ContainsKey("IsSubscribed"))
{
bool WasPurchased = false;
Task.Run(async () =>
{
WasPurchased = await BillingTasks.WasItemPurchasedAsync();
}).Wait();
Application.Current.Properties["IsSubscribed"] = WasPurchased;
}
....
}
但是当我将相同的代码放在 OnStart() 中时,任务似乎永远不会结束并且它崩溃了。那么造成这种不同行为的原因可能是什么?
protected override void OnStart()
{
....
if (!Properties.ContainsKey("IsSubscribed"))
{
bool WasPurchased = false;
Task.Run(async () =>
{
WasPurchased = await BillingTasks.WasItemPurchasedAsync();
}).Wait();
Application.Current.Properties["IsSubscribed"] = WasPurchased;
}
....
}
【问题讨论】:
标签: c# asynchronous xamarin.forms xamarin.android