DisplayAttribute名称属性在Silverlight中不工作 [英] DisplayAttribute name property not working in Silverlight

查看:83
本文介绍了DisplayAttribute名称属性在Silverlight中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定 DataGrid.ItemsSource 属性设置为列表与LT; PersonDetails> 对象。我通过让DATAS Silverlight启用WCF服务。因此, PersonDetails 类是在Web项目实施。每个DataGrid的标题文本正在改变,因为我想如果类位于Silverlight项目。但我不能在Web服务使用这个类。的唯一的解决办法是将添加同一类的两个项目。的但是,有没有其他办法?

I am binding DataGrid.ItemsSource property to the List<PersonDetails> object. I am getting datas through Silverlight-enabled WCF Service. So the PersonDetails class is implemented in Web Project. Each DataGrid's header text is changing as i want if the class is located in Silverlight project. But then I can not use this class in the web service. The only solution is to add same class in to the both of the projects. But, is there any other way?

类看起来像:

[DataContract]
public class PersonGeneralDetails
{
    // Properties

    [DataMember]
    [DisplayAttribute(Name = "Sira")]
    public int RowNumber { get; set; }

    [DataMember]
    [DisplayAttribute(Name = "Seriyasi")]
    public string SerialNumber { get; set; }
}

似乎属性在Web项目中产生。我知道我可以使用DataGrid的事件更改标题文本。但我想使它工作使用属性。

It seems attributes aren't generated in web project. I know that I can change header text using DataGrid events. But i want to make it work using attributes.

推荐答案

问题的是WCF DataContract 是一个可互操作的机制,可以跨语言和平台使用。

The problem is the WCF DataContract is an inter-operable mechanism that can be used across languages and platforms.

如果你看一看由的DataContractSerializer (或code在 System.Runtime.Serialization.dll <生成的序列化数据/ code>,具体 InternalWriteObjectXyz()方法),你会看到,它仅仅值序列化到一个简单的XML消息。涉及到.NET Framework什么也不会有那么各种属性,定制和编译器生成,将被剥离出来,甚至不会被客户接受。

If you take a look to serialized data generated by the DataContractSerializer (or its code in System.Runtime.Serialization.dll, specifically InternalWriteObjectXyz() methods) you'll see that it merely serializes values into a simple XML message. Nothing related to .NET Framework will be there so all kind of attributes, both custom and compiler generated, will be stripped out and won't even received by the client.

它可以创建数据的复制并从服务器发送他们的客户端,客户端将然后创建一个新类具有相同签名即可。注意:a。新CLASS 具有相同签名,不会只是一个新的对象原班

It works creating a copy of your data and sending them from server to client, clients will then create a new class with the same signature. Note: a NEW CLASS with the same signature, NOT JUST A NEW OBJECT of the original class.

当然,也有一些解决方法这一点。你可以写的您自己的序列(见这个职位上的SO 一个例子)或您自己的 ISerializationSurrogate

Of course there are some workaround for this. You may write your own serializer (see this post on SO for an example) or your own ISerializationSurrogate.

如果你的部署/共享程序集,以你的客户,你有一个很好的解决方法:只需将它们部署和的DataContractSerializer 将建立合适的对象客户端上(正好同一个,你必须在服务器上,其所有属性)。只要记住:

If you can deploy/share your assemblies to your clients you have a nice workaround: just deploy them and DataContractSerializer will build the right object on your clients (exactly the same one you had on the server, with all its attributes). Just remember that:


  • 如果自定义属性来自运行时间值(例如,由于本地化的),那么他们会在客户端上得到解决,而不是在服务器上(因为属性会的创建的客户端,它们的值将不被包含在XML消息)。

  • 在你需要添加一个引用到包含你的类型的程序集的客户端应用程序。

  • 当您添加服务引用,你必须指导VS使用它们(或它会创建代理),在服务参考设置对话框中选择再利用类型的引用的程序集(你可以限制这只是您要共享组件)。

  • If custom attributes comes from run-time values (for example because of localization) then they'll be resolved on the client, not on the server (because attributes will be created on the client, their values won't be included in the XML message).
  • In the client application you need to add a reference to the assembly that contains your types.
  • When you add your service reference you have to instruct VS to use them (or it'll create proxies), in the Service Reference Settings dialog select Reuse types in referenced assemblies (you can limit this to only assemblies you want to share).

这篇关于DisplayAttribute名称属性在Silverlight中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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