如何解决全局变量,用于在C#中存储等待语句
我有一个等待语句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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。