locked
Await View Model build

    Question

  • Newbie question. I have not found an example to AWAIT on building a View Model like this:

                    BookViewModel thisBookVM = new BookViewModel(); // want to await this

    var bokName = thisBoom.VM.Name

    Can you tell me how to wait for the VM to build before executing the next statement? Thanks.


    FS

    Thursday, February 05, 2015 3:29 PM

Answers

  • You cannot await constructors nor make them async so in order to construct an object asynchronously you will have to wrap the creation in a Task:

    BookViewModel viewModel = await Task.Run<BookViewModel>(() => new BookViewModel());
    

    Please remember to mark helpful posts as answer to close your threads once your original question has been answered and then start a new thread if you have a new question.
    • Marked as answer by Frank14303 Thursday, February 05, 2015 4:57 PM
    Thursday, February 05, 2015 3:57 PM

All replies

  • You cannot await constructors nor make them async so in order to construct an object asynchronously you will have to wrap the creation in a Task:

    BookViewModel viewModel = await Task.Run<BookViewModel>(() => new BookViewModel());
    

    Please remember to mark helpful posts as answer to close your threads once your original question has been answered and then start a new thread if you have a new question.
    • Marked as answer by Frank14303 Thursday, February 05, 2015 4:57 PM
    Thursday, February 05, 2015 3:57 PM
  • Perfect. Thank you for your quick response! 

    FS

    Thursday, February 05, 2015 4:57 PM