全局变量,用于在C#中存储等待语句

问题描述

我有一个等待语句await SecureStorage.GetAsync("RegisteredUserID");,该语句将用户ID存储在Xamarin安全存储中。 SetAsynchGetAsynch只能与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