WCF RIA Services – loading data and binding

riasilverlightwcf

I've just been toying around with the new WCF RIA Services Beta for Silverlight this evening. So far it looks nice, but I've come across a few barriers when trying to retrieve data and exposing it to the UI via binding.

First of all, how am I able to get a single integer or string value from my service? Say if I have this method on my domainservice:

public int CountEmployees()
{
return this.ObjectContext.Employees.Count();
}

How am I able to make a call to this and bind the result to, say, a TextBlock?

Also, is there any way to make a custom layout for binding data? I feel a little "limited" to ListBox, DataGrid and such. How is it possible to, i.e., make a Grid with a stackpanel inside and have some TextBlocks showing the bound data? If it's possible at all with WCF RIA Services 🙂

Thanks a lot in advance.

Best Answer

To do custom methods you can use the Invoke attribute. In the server side you declare in a domain service like this

[EnableClientAccess]
public class EmployeesService : DomainService
{
    [Invoke]
    public int CountEmployees() 
    {
        return this.ObjectContext.Employees.Count(); 
    }
}

And in your Client-side you can use it like this

EmployeesContext context = new EmployeesContext();
InvokeOperation<int> invokeOp = context.CountEmployees(OnInvokeCompleted, null);

private void OnInvokeCompleted(InvokeOperation<int> invOp)
{
  if (invOp.HasError)
  {
    MessageBox.Show(string.Format("Method Failed: {0}", invOp.Error.Message));
    invOp.MarkErrorAsHandled();
  }
  else
  {
    result = invokeOp.Value;
  }
}

For the second question, you are not limited with binding. The object you get from your context can be binded with any elements you want.

Related Topic