0

I have an await statement await SecureStorage.GetAsync("RegisteredUserID");, which stored the User Id, in the Xamarin Secure Storage. The SetAsynch and GetAsynch can be only used with await in Asynch Function.

But, In my App, in App.xml.cs file, there is an Synchronize Function, where I want to get the value from Secure Storage and use as the value for options.AgentName.

It seems like the Global Variable is one of the Solution. How could I achieve this?

public static IHostBuilder BuildHost(Assembly platformSpecific = null) =>
            XamarinHost.CreateDefaultBuilder<App>()
                .ConfigureServices((_, services) =>
                {
                    services.AddAriesFramework(builder => builder.RegisterEdgeAgent(
                        options: options =>
                        {
                            options.AgentName = "Mobile Holder";

                            // Code
                });

UPDATE

public string UserId { get; private set; }

protected override async void OnStart()
{
    UserId = await SecureStorage.GetAsync("RegisteredPIN");
}
  • 1
    just create a property on your App class – Jason Sep 23 '20 at 19:47
  • `Application.Current.Properties["RegisteredPIN"] = await SecureStorage.GetAsync("RegisteredPIN");` makes the `public App()` red.. how could I use the Await Statement in `App()` (which is non-asynch)... –  Sep 23 '20 at 19:54
  • 1
    create a C# property, and then set if in the OnStart method, not the constructor – Jason Sep 23 '20 at 19:56
  • 1
    https://stackoverflow.com/questions/22628087/calling-async-method-synchronously – DCCoder Sep 23 '20 at 19:56
  • @Jason I add the code above, kindly have a look.. I created the property and set it in the OnStart Method.. how could I use it value for `options.AgentName`... thanks :) –  Sep 23 '20 at 20:08

1 Answers1

-2

create a public property in App and set it's value in OnStart

public string UserId { get; private set; }

protected override async void OnStart()
{
    UserId = await SecureStorage.GetAsync("RegisteredPIN");
}

then anywhere in your app you can access the value

((App)App.Current).UserId
Jason
  • 86,222
  • 15
  • 131
  • 146
  • 1
    Questionable suggestion - first of all this is exactly the same code show in the question... and it does not in any way guarantee that `UserId` will be available when needed (nor give any way to know if it actually obtained the value or not)... – Alexei Levenkov Sep 23 '20 at 20:40