Skip to content

Calculated Properties

Dmitry Shechtman edited this page Mar 26, 2017 · 5 revisions
  1. Install Ditto.AsyncMvvm.Calculated.

  2. Derive your view model from CalculatedAsyncBindableBase:

    using Ditto.AsyncMvvm.Calculated;
    
    class UniversalAnswerViewModel : CalculatedAsyncBindableBase
    {
    }
  3. Implement the asynchronous method for calculating the value of the trigger property:

        private async Task<int?> GetAnswerAsync()
        {
            await Task.Delay(TimeSpan.FromDays(7500000 * 365.25));
            return 42;
        }
  4. Define the trigger property:

        public int? Answer
        {
            get { return Property.Get(GetAnswerAsync); }
        }
  5. Define the calculated property:

        public string AnswerString
        {
            get
            {
                return Property.Calculated(() => Answer.HasValue ? Answer.Value.ToString() : null);
            }
        }

Calculated Properties Copyright (c) 2014 StephenCleary

Clone this wiki locally