Sending Objects from .ASPX page to Silverlight
May 28, 2009 at 10:55 pm Leave a comment
Sending Simple User Defined Objects from ASPX page to Silverlight Application.
This article describes how to send a user defined object created in ASP.Net application across Silverlight application. We will walk though creating a Simple User Defined object in ASPX page called Person and another object called People containing the list of Person.
We will use XMLSerializer to serialize the People class and add the xml content to the InitParametes of the <ASP:Silverlight /> object.
We will then extract the xml from InitParameters in the StartUp_Application event of the Silverlight and fill the data back in the People Class defined in Silverlight application as well.
Creating ASP.Net application to create a list of CustomObject
- Create a Silverlight application and choose ASP.Net Web Site to host the Silverlight application.
- Open the ProjectName.Web project and open the Silverlight test page.
- Add a server tag in the .aspx page and add the code to define the Person.
- Mark the class Serializable.
- Create another class People which contains the list of Person.
- Mark this class as Serializable too.
- Create an object of People and add the Person to list of Person.
<Serializable()> _
Public Class Person
Implements System.ComponentModel.INotifyPropertyChanged
Public Sub New()
End Sub
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs(info))
End Sub
Private _FirstName As String
Public Property FirstName() As String
Get
Return _FirstName
End Get
Set(ByVal value As String)
_FirstName = value
NotifyPropertyChanged(“FirstName”)
End Set
End Property
Private _LastName As String
Public Property LastName() As String
Get
Return _LastName
End Get
Set(ByVal value As String)
_LastName = value
NotifyPropertyChanged(“LastName”)
End Set
End Property
Private _Age As Integer
Public Property Age() As Integer
Get
Return _Age
End Get
Set(ByVal value As Integer)
_Age = value
NotifyPropertyChanged(“Age”)
End Set
End Property
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
<Serializable()> _
Public Class People
Private _Persons As System.Collections.Generic.List(Of Person)
Public Sub New()
Persons = New System.Collections.Generic.List(Of Person)
End Sub
Public Property Persons() As System.Collections.Generic.List(Of Person)
Get
Return _Persons
End Get
Set(ByVal value As System.Collections.Generic.List(Of Person))
_Persons = value
End Set
End Property
End Class
Serializing the list of CustomObject and setting it to InitParameters
- Serialize it using XMLSerializer.
- Using the StringWriter class, write the serialized string to a Serialized variable.
- Set the InitParameters of Silverlight object to “People=” + StringWriterObject.ToString
Dim xmlFrmtr As New System.Xml.Serialization.XmlSerializer(ppl.GetType)
Dim txtWrt As New System.IO.StringWriter()
xmlFrmtr.Serialize(txtWrt, ppl)
Xaml1.InitParameters = “People=” + txtWrt.ToString
Accessing the InitParameters in the Silverlight application.
- Open the Silverlight application and open the App.Xaml.Vb file
- In the App Class, add a public property called InitParams as Dictionary of string, string.
- In the Application_Startup event set the InitParams equal to e.InitParams
- In the Page_Loaded event of Siliverlight in “Page.Xaml.Vb”, create an instance of Application class and assign Application.Current to it.
- Create a StringReader and read all the content of People, which is a XML document.
- Use the XML Reader to read the xml from the String Reader.
- Extract the values from XmlReader and parse it into the People/Person class defined in the Silverlight Application.
Dim app As App = Application.Current
Dim xrDr As XmlReader = XmlReader.Create(New System.IO.StringReader(app.InitParams(“People”)))
Dim ppl As New People()
While xrDr.Read()
If xrDr.IsStartElement And xrDr.Name = “Person” Then
Dim prsn As New Person()
xrDr.ReadToFollowing(“FirstName”)
prsn.FirstName = xrDr.ReadElementContentAsString()
xrDr.ReadToFollowing(“LastName”)
prsn.LastName = xrDr.ReadElementContentAsString()
xrDr.ReadToFollowing(“Age”)
prsn.Age = xrDr.ReadElementContentAsInt
ppl.Persons.Add(prsn)
End If
End While
Entry filed under: Silverlight. Tags: Initializing Silverlight2.0 with parameters, Sending Parameters to Silverlight, Silverlight DataBinding, Silverlight DataGrid, Silverlight InitParams.
Trackback this post | Subscribe to the comments via RSS Feed