WCF RIA 服务 - 加载数据和绑定 [英] WCF RIA Services - loading data and binding

查看:23
本文介绍了WCF RIA 服务 - 加载数据和绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今晚我一直在玩弄 Silverlight 的新 WCF RIA 服务测试版.到目前为止,它看起来不错,但在尝试检索数据并通过绑定将其暴露给 UI 时,我遇到了一些障碍.

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:

公共 int CountEmployees(){返回 this.ObjectContext.Employees.Count();}

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

我怎样才能调用这个并将结果绑定到一个 TextBlock?

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

另外,有没有办法为绑定数据制作自定义布局?我觉得对 ListBox、DataGrid 等有点限制".怎么可能,即,在里面制作一个带有堆栈面板的网格并让一些文本块显示绑定数据?如果可以使用 WCF RIA 服务 :)

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 :)

非常感谢.

推荐答案

您可以使用架构 classname.shared.cs 命名您的类,并且此代码也将在 Silverlight 应用程序中可用.

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

使用 Silverlight/WPF 数据绑定引擎,您可以使用数据网格/列表框容器和常规控件(如文本框/标签)构建任何精美的布局,并应用您自己的样式/皮肤 - 示例.

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.

编辑

共享代码不能包含任何与数据库相关的函数,只能包含一些简单的计算.如果要从服务器检索此值,则需要进行 WCF 方法调用.

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.

在服务器端创建 DomainService 实现:

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;
}

在 Silverlight 客户端提供 HelloWorld 类的所有脏活都是由 Visual Studio 完成的.检查隐藏的生成代码文件夹.

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

[Invoke] 属性在最新版本的 RIA 服务中已过时.

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

这篇关于WCF RIA 服务 - 加载数据和绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆