Passing data and settings to the Silverlight application
Creating the ImagesCollection property
This property is of type string. It returns the serialized value of the GetImages method. To serialize the value, you must use theJavaScriptSerializer class. To create the property, perform the following:
-
Add the using System.Web.Script.Serialization; statement to the Book.cs file.
-
Use the following lines of code to define the property:
private string imagesCollection;
[Browsable(false)]
protected string ImagesCollection
{
get
{
if (String.IsNullOrEmpty(this.imagesCollection))
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
this.imagesCollection = serializer.Serialize(this.GetImages());
}
return this.imagesCollection;
}
}
To prevent the property from appearing in the property editor, you must mark it with the [Browsable(false)] attribute.
Creating the Book settings properties
To create the properties for the Book settings, you must perform the following:
-
Create enumerator classes for the return types of the properties.
The enumerators ensure that the values passed from the server are suitable for the properties of the RadBook control in the Silverlight application. Place the code for the enumerators in a separate file or in the Book.cs file together with Book class.
public enum PagePosition
{
Right,
Left
}
public enum HardPages
{
All,
Custom,
First,
FirstAndLast,
Last,
None
}
public enum PageFlipMode
{
DoubleClick,
None,
SingleClick
}
-
Create FirstPagePosition, HardPages, PageFlipMode and RightPageIndex properties.
private PagePosition firstPagePosition = PagePosition.Right;
private HardPages hardPages = HardPages.FirstAndLast;
private PageFlipMode pageFlipMode = PageFlipMode.SingleClick;
private int rightPageIndex = 0;
public PagePosition FirstPagePosition
{
get
{
return this.firstPagePosition;
}
set
{
this.firstPagePosition = value;
}
}
public HardPages HardPages
{
get
{
return this.hardPages;
}
set
{
this.hardPages = value;
}
}
public PageFlipMode PageFlipMode
{
get
{
return this.pageFlipMode;
}
set
{
this.pageFlipMode = value;
}
}
public int RightPageIndex
{
get
{
return this.rightPageIndex;
}
set
{
this.rightPageIndex = value;
}
}
-
Mark these properties with the [Category("Book Properties")] attribute.
The properties appear under the same category in the property editor.
Passing the values to the client component
To pass the values of the properties to the client component, you must perform the following steps inside the GetScriptDescriptorsoverride:
-
Define a ScriptDescriptor object for the Book control.
public override IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
{
var descriptor = new ScriptControlDescriptor(this.GetType().FullName, this.ClientID);
return new[] { descriptor };
}
-
Add the value of the ImagesCollection property to it.
public override IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
{
var descriptor = new ScriptBehaviorDescriptor(this.GetType().FullName, this.ClientID);
descriptor.AddProperty("imagesCollection", this.ImagesCollection);
return new ScriptDescriptor[] { descriptor };
}
-
Add the values of the FirstPagePosition, HardPages, PageFlipMode and RightPageIndex properties to a collection, serialize the collection and add the result as a property.
public override IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
{
var descriptor = new ScriptBehaviorDescriptor(this.GetType().FullName, this.ClientID);
descriptor.AddProperty("imagesCollection", this.ImagesCollection);
var properties = new
{
firstPagePosition = this.FirstPagePosition.ToString(),
hardPages = this.HardPages.ToString(),
pageFlipMode = this.PageFlipMode.ToString(),
rightPageIndex = this.RightPageIndex,
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
var serializedProperties = serializer.Serialize(properties);
descriptor.AddProperty("properties", serializedProperties);
return new ScriptDescriptor[] { descriptor };
}
-
Add the value of the ID of the Silverlight object as a property.
public override IEnumerable<System.Web.UI.ScriptDescriptor> GetScriptDescriptors()
{
var descriptor = new ScriptBehaviorDescriptor(this.GetType().FullName, this.ClientID);
descriptor.AddProperty("imagesCollection", this.ImagesCollection);
var properties = new
{
firstPagePosition = this.FirstPagePosition.ToString(),
hardPages = this.HardPages.ToString(),
pageFlipMode = this.PageFlipMode.ToString(),
rightPageIndex = this.RightPageIndex,
};
JavaScriptSerializer serializer = new JavaScriptSerializer();
var serializedProperties = serializer.Serialize(properties);
descriptor.AddProperty("properties", serializedProperties);
descriptor.AddProperty("silverlightBookObjectId", "silverlightBook");
return new ScriptDescriptor[] { descriptor };
}