In my solution, I have a Silverlight application using WCF services and a single DataModels.cs file with contents similar to the following. The only thing to remember is to add the WindowsBase reference to your WCF service project in order to use the same ObservableCollection<> template object.
(in my case, I put DataModels.cs in the WCF project, and created a virtual file link to it from the Silverlight project)
This is a sample of the code I use:
____________________________________________________ ____________________________________________
// <...other using statements...> using System.Collections.ObjectModel; // ObservableCollection<> is added via WindowsBase (WindowsBase.dll). #if !SILVERLIGHT [DataContract] public partial class CPPLoginState { [DataMember] public bool Ok { get; set; } [DataMember] public string UserName { get; set; } [DataMember] public Int32 UserID { get; set; } [DataMember] public ObservableCollection Roles { get; set; } } #endif public partial class CPPLoginState { public CPPLoginState() { Ok = false; UserName = ""; UserID = 0; Roles = new ObservableCollection(); } }
________________________________________________________________________________________________
The trick here is in the Silverlight proxy’s use of the “partial” modifier for it’s class definitions. This allows “adding” code to them.
I must say, it only took one week working with Silverlight (from knowing nothing, not even WPF) and I’m almost done with a fairly complicated control library and host application. Silverlight + WCF really does == RAD development.