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

  • Home
  • SEARCH
  • 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 8999603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:07+00:00 2026-06-16T00:12:07+00:00

I have a view model with a constructor that requires a service. I’m using

  • 0

I have a view model with a constructor that requires a service. I’m using GalaSoft’s MvvmLight which uses a service locator to wire up the view to the view model.

SimpleIOC handles providing the service to the viewmodels constructor fine, but I somehow need to populate my viewmodel with data from a data source . My Viewmodel looks a like this:-

    public class MainPageViewModel : ViewModelBase
{
    private readonly GroupService _groupService;
    private readonly GroupFactory _groupFactory;
    private readonly ObservableCollection<GroupVM> _groupVms = new ObservableCollection<GroupVM>();


    public MainPageViewModel(Domain.Services.GroupService groupService, VMFactories.GroupFactory groupFactory)
    {
        _groupService = groupService;
        _groupFactory = groupFactory;
    }

    public async Task Init()
    {
        var groups = await _groupService.LoadGroups();
        foreach (var group in groups)
        {
            GroupVms.Add(_groupFactory.Create(group));
        }
    }

    public ObservableCollection<GroupVM> GroupVms { get { return _groupVms; } }
}

Somehow the init method needs to be called an awaited, but I don’t know how best to do this? I can think of three options:-

  1. I just call Init on the constructor, but not await it (I know thats really bad practice)
  2. I call Init on ViewModelLocator object, but since I can’t return a task I again can’t await the init
  3. On the view’s load I cast the DataContext to some sort of IAsyncViewmodel and await the init method.

I have used option 3 in a previous windows 8 store project but it just feels wrong. Any advice would be really appreciated!

Thanks

Ross

  • 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-16T00:12:08+00:00Added an answer on June 16, 2026 at 12:12 am

    I am curious why you consider not awaiting an async call a bad practice. In my opinion, it is not bad as long as you know what that means, that the call will be executed in the background and might return any time.

    Typically what I do is that I call the async method in the constructor for design time data creation purpose and I don’t await it. I just take care of raising the PropertyChanged and CollectionChanged events where needed for the bindings to be updated, and that does the trick.

    Cheers
    Laurent

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model that is displayed in a view that uses uploadify.
I have a view model that looks like this public class ViewModelRound2 { public
I have this view @model IEnumerable<ViewModelRound2> ... <snip> ... @{ using (Html.BeginForm(new { round1Ring3Bids
I have a view model with a couple of properties that are not displaying
I have a view model with a property Fields which is an ObservableCollection<FieldVM> .
I have a view model that contains a Product class type and an IEnumerable<
In my application, we have a view model object from which we're retrieving its
I have an event that I am subscribing to in a View Model. The
In my backbone.Marionette application I have a Model that requires an Id attribute to
When I have a View Model setup with an ImportingConstructor that takes a single

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.