Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:18:58+00:00 2026-06-17T17:18:58+00:00

I want to load some data in the constructor of a ViewModel but due

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:19:00+00:00Added an answer on June 17, 2026 at 5:19 pm

    I wrote a recent blog post about async in constructors.

    In short, I prefer an async factory method:

    public sealed class MyViewModel : INotifyPropertyChanged
    {
      private readonly DataService _service;
    
      private MyViewModel(DataService service)
      {
        _service = service;
      }
    
      private async Task InitializeAsync()
      {
        var result = await _service.GetData(); // async initialization
    
        Data = result; // update data-bound properties with the results
      }
    
      // Data triggers INotifyPropertyChanged on write
      public string Data { get { ... } set { ... } }
    
      public static async Task<MyViewModel> CreateAsync()
      {
        var ret = new MyViewModel();
        await ret.InitializeAsync();
        return ret;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use an HTTPService to load some data (number of columns and
I have a UINavigationController consisting of a tableview I want to load some data
I want to load some .css files to my Django project but I don't
I have huge amount of data locally, but don't want to load all to
Here is my code I want to load some data by Jsonp and display
I want to load data to an instance of an object using its constructor
In my application,I want load some data to the database at the midnight each
I want to load some data from mysql into my cocoa application view before
I want to load some helper in a model. How to do this? Tried
i want to load some photos into my sd card image in the emulator.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.