Reputation: 7800
I'm trying to load some data (from a configuration file) at startup. The onlys way to acces file in win 10 uwp is to use async method.
Great, but:
How can'I call an async method in a constructor (this is not possible, I know) or any equivalent. Here is my actual code.
sealed partial class App : Application
{
public ITrackThatContext Context { get; private set; }
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
Context = await LocalFileTrackThatContext.CreateAsync(null);
}
}
I can call the Context = await LocalFileTrackThatContext.CreateAsync(null);
from a button on the first page of the app, but I'm sure there is better to do. Is there any entry point for launching a task from the startup process. I read startup stages, but I can't figure out where to load my conf.
Upvotes: 1
Views: 568
Reputation: 9990
While there could be other places, the most convenient is in OnNavigatedTo
method of the first page that you load.
EDIT: If you need this to be done just once you can have one Page with just Frame
inside it from which you then navigate to other pages. Or you can somewhere store a bool value whether the operation is completed or not and act based upon that.
Upvotes: 1
Reputation: 2128
Define a callback method and provide that to Task.ContinueWith():
LocalFileTrackThatContext.CreateAsync(null).ContinueWith(contextLoaded);
In this case, the rest of the constructor should assume Context is not yet set - anything that needs to wait until Context is known will happen in the callback.
The callback method is passed a Task argument, and it can get .Result and .Status from there.
Upvotes: 1