0

I'm using a TwoWay databinding for a collection of objects, but when the field is edited, it needs to update to the backend model, which in turn need to call an async library code, but the data binding are bound to properties not methods, so my questions are:

1. is there a way to call async methods in Set method of the property to do some async work 2. or is there a way to make databinding bind on methods rather than property, so that it can call the methods just like event handlers.

///

public class Model
{
    public int Field
    {
        get { return field; }
        set { field = value; await UpdateValue(field); } // won't compile
    }
}

EDIT: I understand there is no support of async properties, also this question isn't really how to do async properties in a language as most of the answers focused. This question focus on how to implement this specific design problem, and I can imagine this would rise a lot when using databinding. Suppose a data item is bind to a network service or database, then in the update path, one will need to do await socket.SendAsync(value) or await db.SaveChanges(value). What is the proper way to solve this situation?

fluter
  • 13,238
  • 8
  • 62
  • 100

2 Answers2

1

You can't await a method in a property. You should probably ask yourself whether you should replace the property with a command that kicks off the asynchronous background operation. A property is in general only supposed to get and set the value of a backing field and should execute immediately.

When it comes to implementing asynchronous data-bound properties you could refer to @Stephen Cleary's MSDN Magazine article on the subject.

Async Programming : Patterns for Asynchronous MVVM Applications: Data Binding: https://msdn.microsoft.com/en-us/magazine/dn605875.aspx

mm8
  • 163,881
  • 10
  • 57
  • 88
  • so do you suggest something like fire off a background task in the set method of the property: Task.Run(do_work); ? – fluter Aug 29 '17 at 11:54
  • That's the simplest solution if you just want to fire-and-forget some operation and don't care about any return value. But what about using a NotifyTaskCompletion as suggested in the article that I linked to? Or call your async method in a command instead of doing it in a property setter. – mm8 Aug 29 '17 at 12:00
  • I'm not sure I understand it right, the property is set by databind code when it is edited, so who will call this command in this situation? – fluter Aug 29 '17 at 12:06
  • You obviosuly need to re-think your approach since you *can't* await a method in a property. – mm8 Aug 29 '17 at 12:09
  • I have given you some options. But you cannot bind to a method like you bind to a property. But a method can set a property. – mm8 Aug 29 '17 at 14:10
1

There is no way to await a method call in a property.

Only way I could imagine to fix this, is to call a method from the setter and in this method make the async call to the library code.

This thread is similar to your question: How to call an async method from a getter or setter?

Eike B
  • 231
  • 1
  • 7