I want to load some data in the constructor of a ViewModel but due to WinRT's async nature I am forced to use async methods. Unfortunately I cannot have an async constructor so I am trying to use the asynchronous method as a synchronous method. I'm sure there is a much better way of loading data (async) on application load but my mind is drawing a blank at the moment.
I'm either looking for a way to fix my app using the line of thought i'm going down, or to fix this permanently using a more appropriate method.
The code is very simple (even missing the ViewModel) just to demonstrate the issue I'm facing.
public sealed partial class MainPage : Page
{
public string Data { get; set; }
public DataService _dataService { get; set; }
public MainPage()
{
this.InitializeComponent();
_dataService = new DataService();
var t = _dataService.GetData();
Data = t.Result;
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
}
public class DataService
{
public async Task<string> GetData()
{
//Force async
await Task.Delay(1);
return "Hello";
}
}
Kind Regards