Sunday, January 17, 2010

Slide Show in Silverlight


When my friends come and ask me how to start with Silverlight, I always point them to http://silverlight.net/ But when they ask me to give them some kind of a task so that they can have hands on, then I always tell them to make a simple slide show. Slide show is a thing which is very easy and which can be made equally complex. I find that from the making of this simple show to its conversion to a complex carousel, we get to learn Silverlight and in turn gain confidence over it.


For a beginner it’s always good to start with some static images put in some layout element, when we explore further to see what will be the best container for a stack of images we in turn explore different layouts. Then to make things look better and to add a Silverlight touch to it, I tell them to add little animations on mouse over and out events. Then I tell them to add few buttons and control the slide show using them. By now they ask me how they can do the entire thing dynamically. Then a series of events and changes are done on the same old slide show and in the end a wonderful carousel is made. If you are a beginner in Silverlight then I would suggest you to start learning it by making a slide show. People have made thousands of slide shows but each has a different touch. Here is my version for you to start with.


In this I am locally maintaining an XML that has links to images of some cute dogs. I am reading this xml and then displaying them in the age old filmstrip fashion. To read the xml I have used Linq. The slide show is controlled by a pair of forward and backward buttons.







So, now when everything is all set, why don’t you Download Source Code and get started!

Sunday, January 10, 2010

Reading xml from a SharePoint server

Every now and then we have to send and receive data through an xml or an RSS feed. Some of these xml files can be kept somewhere at the server or can be kept locally. If the xml is stored at a different location then you have to write a web service that would call it and put a Client access policy there. I had a requirement to read an xml file stored at a sharepoint server. Here is how I have done it without web services.
List<string> itemDetailsList = new List<string>();
 
List<string> itemTitleList = new List<string>();
 
List<string> itemSourceList = new List<string>();
 
private XNamespace z = "#RowsetSchema";
 
public MainPage()
 
{
 
InitializeComponent();
 
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
 
}
 
void MainPage_Loaded(object sender, RoutedEventArgs e)
 
{
 
//this xml path will be kept at a SharePoint server and the uri will have the address of the SharePoint Server.
 
Uri absUrl = new Uri("http://yourSite/_vti_bin/owssvr.dll?Cmd=Display&List=%7BCA37%2D7F%2D408CBF6%2D059D&XMLDATA=TRUE", UriKind.RelativeOrAbsolute);
 
WebClient xmlClient = new WebClient();
 
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
 
xmlClient.DownloadStringAsync(absUrl);
 
}
 

 
void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 
{
 

 
if (e.Error == null)
 
{
 
XDocument doc = XDocument.Parse(e.Result.ToString());
 
var docNode = from item in doc.Descendants(z + "row")
 
select new
 
{
 
ItemTitle = item.Attribute("ows_NameOrTitle") == null ? string.Empty : item.Attribute("ows_NameOrTitle").Value,
 
ItemDetails = item.Attribute("ows_Details") == null ? string.Empty : item.Attribute("ows_Details").Value,
 
ItemSource = item.Attribute("ows_RequiredField") == null ? string.Empty : item.Attribute("ows_RequiredField").Value
 
};
 
foreach (var itemContent in docNode)
 
{
 
itemTitleList.Add(itemContent.ItemTitle);
 
itemDetailsList.Add(itemContent.ItemDetails);
 
itemSourceList.Add(itemContent.ItemSource);
 
}
 
}
 
else
 
{
 
MessageBox.Show("error while reading from the server");
 
}
 
}