Skip to content

Instantly share code, notes, and snippets.

@StephenCleary
Forked from BrianJVarley/AwaitableMessages.cs
Last active December 16, 2015 13:09
Show Gist options
  • Save StephenCleary/139f6b313788032fe9c6 to your computer and use it in GitHub Desktop.
Save StephenCleary/139f6b313788032fe9c6 to your computer and use it in GitHub Desktop.
internal class AwaitableMessages
{
public static Task<T> NextMessageAsync<T>()
{
var tcs = new TaskCompletionSource<T>();
Messenger.Default.Register<T>(null, item => tcs.TrySetResult(item));
return tcs.Task;
}
}
namespace MongoDBApp.ViewModels
{
[ImplementPropertyChanged]
public class CustomerOrdersViewModel : IPageViewModel
{
private IDataService<OrderModel> _orderDataService;
public CustomerOrdersViewModel(IDataService<OrderModel> orderDataService)
{
this._orderDataService = orderDataService;
this.Initialization = InitializeAsync();
}
#region properties
public string SelectedCustomerEmail { get; set; }
public ObservableCollection<OrderModel> CustomerOrders { get; set; }
public Task Initialization { get; set; }
#endregion
#region methods
private async Task InitializeAsync()
{
var customer = await AwaitableMessages.NextMessageAsync<CustomerModel>();
SelectedCustomerEmail = customer.Email;
await LoadCustomerOrdersAsync(SelectedCustomerEmail);
IsEnabled = true;
}
public async Task LoadCustomerOrdersAsync(string email)
{
var ordersResult = await _orderDataService.GetAllByEmailAsync(email);
CustomerOrders = ordersResult.ToObservableCollection();
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment