views:

1584

answers:

2

Hi all,

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.

+2  A: 

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.

Zied
+1  A: 

You can name your class with schema classname.shared.cs and this code will also available in silverlight application.

Using Silverlight/WPF databinding engine you can build any fancy layout using datagrid / listbox containers and regular controls like textbox/label and apply your own style/skin - Example.

EDIT

Shared code cannot contain any database-related functions, only some plain calculations. If you want to retrieve this value from server then you need to make WCF method call.

At serverside you create DomainService implementation:

   [EnableClientAccess()]
    public class HelloWorld : DomainService
    {
        public string SayHello()
        {
            return "Test";
        }
    }

Then you can use this at client:

    HelloWorld context = new HelloWorld();
    context.SayHello(x => context_SayHelloCompleted(x), null);

void context_SayHelloCompleted(System.Windows.Ria.InvokeOperation<string> op)
{
    HelloTextBlock.Text = op.Value;
}

All dirty work with making HelloWorld class available at Silverlight client is done by Visual Studio. Check hidden generated code folder.

[Invoke] attribute is obsolete in newest release of RIA services.

tomo
Actually, the shared.cs file will be available as it is and it will only copied. i.e : you can't access the database in the methods in this file. So, if you want to query only the count of employees without querying all the data, you shouldn't put it in the shared.cs file
Zied
Thanks Zied. Updated content.
tomo
Thanks guys! Makes perfectly good sense :) So when doing calls to custom methods, it's more like a normal WCF call rather than a "RIA call" since the [Invoke] is obsolete now?
bomortensen