问题描述
我有一个等待语句await SecureStorage.GetAsync("RegisteredUserID");
,该语句将用户ID存储在Xamarin安全存储中。 SetAsynch
和GetAsynch
只能与Asynch Function中的await
一起使用。
但是,在我的应用程序中,App.xml.cs
文件中有一个同步功能,我想在其中从安全存储中获取值并将其用作options.AgentName
的值。
public static IHostBuilder BuildHost(Assembly platformSpecific = null) =>
XamarinHost.CreateDefaultBuilder<App>()
.ConfigureServices((_,services) =>
{
services.AddAriesFramework(builder => builder.RegisterEdgeAgent(
options: options =>
{
options.AgentName = "Mobile Holder";
// Code
});
更新
public string UserId { get; private set; }
protected override async void OnStart()
{
UserId = await SecureStorage.GetAsync("RegisteredPIN");
}
解决方法
在App
中创建一个公共属性,并在OnStart
中设置它的值
public string UserId { get; private set; }
protected override async void OnStart()
{
UserId = await SecureStorage.GetAsync("RegisteredPIN");
}
然后您可以在应用程序中的任何位置访问该值
((App)App.Current).UserId