WCF RIA Services - loading data and binding

2019-06-03 20:06发布

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条回答
混吃等死
2楼-- · 2019-06-03 20:33

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.

查看更多
Emotional °昔
3楼-- · 2019-06-03 20:45

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.

查看更多
登录 后发表回答