DisplayAttribute Name Property Does Not Work in Silverlight

I am binding the DataGrid.ItemsSource property to a List<PersonDetails> object. I get data through WCF service with Silverlight support . Thus, the PersonDetails class PersonDetails implemented in a web project. Each text of the DataGrid header changes as I want if the class is in a Silverlight project. But then I can not use this class in a web service. The only solution is to add the same class to both projects. But is there any other way?

The class is as follows:

 [DataContract] public class PersonGeneralDetails { // Properties [DataMember] [DisplayAttribute(Name = "Sira")] public int RowNumber { get; set; } [DataMember] [DisplayAttribute(Name = "Seriyasi")] public string SerialNumber { get; set; } } 

It seems that attributes are not generated in the web project. I know that I can change the title text using DataGrid events. But I want it to work using attributes.

+6
c # silverlight wcf
source share
1 answer

The problem is that WCF DataContract is an interoperable mechanism that can be used on different languages ​​and platforms.

If you look at the serialized data generated by the DataContractSerializer (or its code in System.Runtime.Serialization.dll , namely the InternalWriteObjectXyz() methods), you will see that it simply serializes the values ​​into a simple XML message. There will be nothing related to the .NET Framework, so all kinds of attributes, both generated by the user and using the compiler, will be deleted and not even received by the client.

It creates a copy of your data and sends it from the server to the client, then the clients will create a new class with the same signature . Note: A NEW CLASS with the same signature is NOT ONLY A NEW OBJECT of the source class.

Of course, there is a workaround for this. You can write your own serializer (see this post in SO format for an example) or your own ISerializationSurrogate .

If you can deploy / share your assemblies for your clients, you have a good way: just deploy them, and the DataContractSerializer build the correct object on your clients (just like you had on the server with all its attributes). Just remember that:

  • If user attributes come from runtime values ​​(for example, due to localization), they will be allowed on the client and not on the server (since the attributes will be created on the client, their values ​​will not be included in the XML message).
  • In the client application, you need to add a link to the assembly containing your types.
  • When you add a link to a service, you must tell VS to use them (or create a proxy), in the Help service settings dialog box, select Reuse types in reference assemblies (you can limit it only to the nodes you want to split).
+3
source share

All Articles