WCF RIA Services - loading data and binding

隐身守侯 提交于 2019-12-06 14:36:37

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!