Vijay Krishna's Notes http://vijaykrishna.posterous.com Most of my notes as a student of computer software and everything around it. posterous.com Tue, 22 Mar 2011 13:20:39 -0700 Retrieving all the Methods in a Service http://vijaykrishna.posterous.com/retrieving-all-the-methods-in-a-service http://vijaykrishna.posterous.com/retrieving-all-the-methods-in-a-service Well, today i was in a real fix. I was asked to work out a manner by which i show all the methods of a service in a combo-box. Now, people told me to ideally have a look at the proxy and type out the method names as they were and then create a static list of strings which i could then bind to the combo-box. WOW!! So simple ain't it?!? "And what if the list of methods were to change", i asked. The reply came, well, you can guess what the reply was. Someone then suggested a method involving WSDL. I do not know what that meant, as the idea was instantly trashed and i was never explained the details. Either way i smelled unnecessary  labor there as well. So i put up a brave face and googled it. I refused to believe that the method list of a given class could not be retrieved. After all that was the crux of my project. So after a little bit of googling and a little asking around from a trusted colleague, i found a way: Type. [sourcecode language="csharp"] public List<string> GetMethodNames(){ Type type = client.GetType(); var methods = type.GetMethods(); List<string> methodNames = new List<string>(); methodNames = methods.select(x=>x.Name).ToList<string>(); return methodNames; } [/sourcecode] So i can actually send in the type of service client (for which the method name list is required) to the GetMethodNames function and get different lists for different services. Thus, allowing me to dynamically get the method lists, which will take care of cases when the service changes the methods in terms of names and numbers. Ofcourse, in case of a Silverlight project, we will have to take care of things like the "Async" prefix. So we just have to remove that. Simple enough. So that is that!! :D

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Tue, 22 Feb 2011 17:32:32 -0800 Calling Async Service Calls Iteratively in Silverlight http://vijaykrishna.posterous.com/calling-async-service-calls-iteratively-in-si http://vijaykrishna.posterous.com/calling-async-service-calls-iteratively-in-si Well, i was posed with a rather strange situation today when i had to make Async Svc Calls from a Silverlight UI code. The issue was that i had to call a single service method in an iterative fashion, ideally in a loop. But the problem was that i was doing a lot of processing in the completed event handlers and the order of the processing was making a difference to my UI. Ideally, had it been sync calls, i would have finished it off in a for loop. And then i was introduced to the concept of Enumerators. Here is a pseudo code snippet for the same: private void _getData() { List<string>.Enumerator enumerator = ListSource.GetEnumerator(); //create the Enumerator If(enumerator.MoveNext()) { _getData(enumerator);//send the enumerator to the _getData function } }   private void _getData(List<string>.Enumerator enumerator) { ServiceClient client = ServiceHelper.GetServiceClient(); client.GetDataCompleted += new EventHandler(retrieveDataCallBack); client.GetDataAsync(arg1,enumerator); //send the enumerator in the userstate }   void retrieveDataCallBack(object sender, GetDataCompletedEventArgs e) { //perfrom operation on e.Result; Before that do a check on e.Error If((e.Userstate as List<string>.Enumerator).MoveNext()) { _getData(e.Userstate as List<string>.Enumerator); } }   It was good to learn something fresh after an eon. :)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Tue, 01 Feb 2011 18:24:13 -0800 Vertical Scrollbar Visibility http://vijaykrishna.posterous.com/vertical-scrollbar-visibility http://vijaykrishna.posterous.com/vertical-scrollbar-visibility So, this is something very basic i learned today. The scrollbar in silverlight appears only when the length of the container is predefined and not depended on its parent container. For example, if you set the grid row height in which you have placed you textbox to "AUTO", you will never be able to see/activate the scrollbar. Simply because the textbox assumes that you have set the parent grid in a manner that will keep the size in check. Instead of activating the Scrollbar in the container, in this case the text box, it will simply stretch the textbox beyond the parent container boundary. Another instance of this is when you set the Gird.RowSpan or Grid.ColumnSpan for the textbox. Again in can never decide when to activate it, as its height is dependent on its parent grid. And the grid itself will keep expanding as the textbox expands as it keeps getting filled with more and more data. This can be avoided if you set the Grid height to "*", in which case it should be able to activate the scroll, because for some reason, with "*" it can pre-compute the size somehow. Having said that, the star(*) will fail to work in case of the Row and Column Spans. Simply because the issue is not with the height, but with the extent of the textbox which is ultimately decided by the parent grid. The best solution is to try and maintain fixed size textboxes for activating the Scrolls. It only makes sense, right? Otherwise why would you need a scroll, if you do not find it important enough to fix a length?

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Fri, 14 Jan 2011 18:15:13 -0800 Dynamically Adding Images in a UI http://vijaykrishna.posterous.com/dynamically-adding-images-in-a-ui http://vijaykrishna.posterous.com/dynamically-adding-images-in-a-ui Now i am sure that there are many ways of doing this but i found a quick and easy method of this for a silverlight UI with a C# code behind. All you need is the File (which is a jpg/png) on your harddisk. So here it is: creating Image from byte[] / file #region Image from FileInfo Image myImage = new Image(); //the image can be a xaml object also FileInfo file = new FileInfo(""); //available from a file browser window BitmapImage bitmapImage = new BitmapImage(); using (Stream stream = file.OpenRead()) { bitmapImage.SetSource(stream); } myImage.Source = bitmapImage; #endregion #region Image from byte[] Image myImage = new Image(); //the image can be a xaml object also byte[] data = null;//provided by service or anyother data source BitmapImage bitmapImage = new BitmapImage(); using (Stream stream = new MemoryStream(data)) { bitmapImage.SetSource(stream); } myImage.Source = bitmapImage; #endregion Here myImage in both the codes is nothing but a UI element being shown on the Screen. You can set its layout and size attributes as you wish and set. In essence you are creating an image in memory using a BitmapImage object and then setting it as the source of the Image object. What is important to note is that Silverlight only recognizes .jpg or .png files. Surprisingly it does not support .gif files. So there you are. A simple, quick and effortless way of showing thumbnails or pictures dynamically in your Silverlight UI. This is what i used, when i was creating an upload utility for an application. Hope this helps. :)

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Wed, 12 Jan 2011 17:58:10 -0800 Trivial Error http://vijaykrishna.posterous.com/trivial-error http://vijaykrishna.posterous.com/trivial-error Well, this is a post about how i made a real silly mistake when i could have easily avoided it. I want to share this experience so that others can gain something from it. So here is the Scenario: A classical integration of the services with the Silverlight UI. All calls are Asynchronous and hence all, of them have a get completed event handler. So, here is a page where i have to make 2 service calls and thus two event handlers. Service Call 1 (SvC1) was to get the data for the UI. Service Call 2 (SvC2) was to get a certain key value pair which would dictate the number and content of a set of check boxes in the UI. So in essence, i was using SvC2 to dynamically set up controls (i was using an items control). Mind you, SvC2 was never returning the controls it self, it was simply giving me the data, i.e the Label/Content property of the check boxes and their tooltip/description property. And yes, the UI has a standard New and Open scenarios. Here is what i did: I made the call to SvC1 first and set up the data context as required, ie the data context would not be set in case of a New Item creation. So, whether or not i was going to bind the data, i would be making the call. Then, irrespective of that, i make the SvC2 to get the data for the check box controls and then with the data-context (if given), and the data from SvC2, i create the controls. Now, why i say "if given" in the previous statement is because some of the check boxes would be clicked if the data context were given, else all of them would be false or not clicked. There is another issue, once i have created the check box controls, it will be very difficult to access them (they are inside the ItemsControl). I would rather make the controls, and assign the data to them in one go. I would not like to split these two tasks, for the sake of simplicity of code and its understandability. Here is what i should have done: I should have made the call to SvC2 first, which would have returned the list of data to be shown in the controls i.e  the check boxes. Then i should have called SvC1 on the Completed (event or on completion) of SvC2, and set the data context, i.e. bound the data with the controls. While doing this i should have set the items source for the check boxes, or, in simple words, i should have created the check boxes dynamically using the information i got in SvC2. This way, a) I would have gotten the data for the controls before hand, b) I would have made the check on the data-context requirement on the completion of SvC2 and before calling SvC1, and end up saving one SvC if required. Otherwise, i was making the SvC1 no matter what and then probably not end up using it and then call SvC2 anyway because it was being called on the get completed event handler of SvC1. c) And finally, i would still be able to create the controls and bind the data to them in one go. You may not be cutting trees by making an extra service call, but you sure are troubling some million electrons for it. :D So take care, use the Service wisely.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Mon, 10 Jan 2011 16:29:56 -0800 Silverlight for Dummies: StackPanel http://vijaykrishna.posterous.com/silverlight-for-dummies-stackpanel http://vijaykrishna.posterous.com/silverlight-for-dummies-stackpanel This is going to be an absolute short post. I plan to couple this with the post on Grid to talk about something a little more complicated, however that would be in the next post, which hopefully will come tomorrow, depending upon how heavy my day. So the stack panel is another nice control which allows you to group controls and map them. However, this does not give you complete control over things as a Grid does. A stack panel is also essentially a container of a basic nature, which can Stack controls horizontally or vertically. Here is a little bit of an overview of the whole syntax: <StackPanel> Children </StackPanel> public class StackPanel : Panel, IScrollInfo // This is its basic definition in C#. <StackPanel x:Name="Layout" Background="Black"> <Button x:Name="Button1" Content="Click me!"></Button> <Button x:Name="Button2" Content="And me!" ></Button> </StackPanel> And this should result in the two buttons stacking one on top of the other i.e vertically. The stack pannel has a Orientation property which has a default Vertical property. If you want the buttons to stack up against each other, just set the Orientation to "Horizontal" and you will have it. <StackPanel x:Name="Layout" Background="Black" Orientation="Horizontal"> <Button x:Name="Button1" Content="Click me!"></Button> <Button x:Name="Button2" Content="And me!" ></Button> </StackPanel> Now there is one thing i would like to mention here about the stack panel which most blogs would not: You have to imagine the StackPanel as a one row(vertical orientation)/one column(horizontal orientation) table. What does this mean? Simple: in the vertical orientation the VerticalAlignment property of the children elements will not have any effect and the the same applies to the HorizontalAlignment when it is Oriented in the horizontal mode. So, the next obvious question is how will the width and height get decided in such an event? Simple agian, it takes the least space, i.e,  width or height required. So, if you have given the control a set width or height it will set it self to just about that, of course, if you have included the margins in the control then it will take that as well. So, whats the catch? In case, you have not assigned any width, or if your buttons do not have any labels of text, then the stackpanel will take the minimum required length i.e. squash the whole control to near zero pixel length and be done with it. There is no point giving the stack panel width or height. It will just increase the width and height of the stack panel. It will not increase the space for your control. Another thing to note is that the stack panel does not allow you to wrap the child controls once it runs out of space. I have tried it, without results. So in case you have managed it do let me know. The thing you can use instead it the WrapPanel which comes in the silverlight toolkit. So, here was a brief about the StackPanel. Hope it is useful. Tomorrow i might jump to slightly more fun stuff. Enjoy!

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu
Wed, 05 Jan 2011 17:53:31 -0800 Silverlight Grid for Dummies http://vijaykrishna.posterous.com/silverlight-grid-for-dummies http://vijaykrishna.posterous.com/silverlight-grid-for-dummies Being a UI designer is not enough. You should be able to render those designs somewhere. Well, i have started my journey as a UI developer and designer in the world of Silverlight. It is a fantastic framework of tools and technology. The best part being that it uses common languages like XML and C#. Well anyhow, i have been working with it for a little over two months now and i have finally recognized its most important control, the <Grid />.So i am going to give a bit of a Silverlight <Grid/> for Dummies. Introduction: To start with lets look at what the Grid has to offer. The Grid is a basic control which is used everywhere to set up the layout of your UI page, window, space, etc. You can define the rows and columns, thus forming cells, whose heights and widths are very customizable and flexible to adjust. It is within these cells that we place all our controls. Hence it is like a Sectional Map to a UI. Now, most of this can be done in the Extensible Application Markup Language or XAML, although i love to get dirty with the code behind and take the pain of defining everything there. This approach might be painstaking and you might even consider me crazy for doing after you realize how difficult it is with C# and how blissful it is with XAML. By the way, stick to the XAML, i use C# to irritate my friends at times ;) . Basics: 0. <Grid/> or <Grid></Grid> This is the Empty Grid or a Single Cell Grid with nothing in it. 1. <Grid.RowDefinitions/> and <Grid.ColumnDefinitions/> These are probably the first things you will write within a Grid element. These tags are used to define the Row and Columns of a Grid. However, note that these are not entirely important. They can be omitted, as every grid by default has one cell. So if you were to add anything within that cell it would just act like a container for the controls or UI elements. Using the Row and Column definitions can be useful as it gives you complete control over your UI Map. You can actually dedicate one cell for each UI element and then all the margin adjustments for that UI control will be based with respect to the boundaries of that grid. 2. <RowDefinition/> and <ColumnDefinition/> These are used inside the <Grid.RowDefinitions/> and <Grid.ColumnDefinitions/> Tags respectively. They are used to define individual Rows and Columns. To get a better understanding of the whole thing have a look at the following code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
</Grid>
Notice how the Widths are set in the ColumnDefinition's and Height is set inside the RowDefinition's. You cannot do things the other way round, simply because it does not make sense.
3. Gird.Row, Gird.RowSpan, Gird.Column, Gird.ColumnSpan

Now that you have defined the Row and Column Definitions its time to use it. All the Row and Column definitions inside the <Grid.RowDefinitions/> and <Grid.ColumnDefinitions/> Tags are maintained as zero indexed Collections or Lists. Thus the 1st Row can be referenced by saying Grid.Row = "0" and so on (similarly for Columns).
So lets fill it up:
<Grid x:Name="grdLayoutRoot" Background="White" Width="400" Height="300" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button x:Name="btnTest" Width="50" Height="30" Content="Text"Grid.Row="0" Grid.Column="0"></Button>
<Rectangle x:Name="rectBlue" Width="50" Height="30" Fill="Blue"Grid.Row="2" Grid.Column="1"></Rectangle>
</Grid>
In this example, i have given the Grid a name or a unique identifier using which you can access its inner controls, in this case the Button and the Rectangle, which have their own names. I have also given the Grid a background (you can do better than white though), a static height and width. An interesting property happens to be the ShowGridLines which you can set to true or false. Setting it to true will mark the Grid lines for better understandability only. Simply because they do not look pretty, unless ofcourse you want to use Grid lines such as those. Here is an example:
[caption id="attachment_58" align="aligncenter" width="404" caption="Grid with the Grid Lines"]
Media_httpvpalepufile_shcfz
[/caption]
Apart from setting the Grid.Row and Grid.Column, you can also set the Grid.RowSpan and Grid.ColumnSpan Properties. Setting these values allows any control to go beyond its established grid boundary.
4. The Auto and *
Now, most of the lengths that you let for Heights and Widths of a Grid Row and Column is in pixels, always. And to the best of my knowledge you cannot not use any other unit of measurement. Apart from that you can use the Auto and * which behave in the following manner:
a. Setting the Height to Auto will adjust the Grid Row to the height of a control with the maximum height in that Row.
b. Setting the Height to * will make the Grid Row take up the Remaining height within the Grid apart from that taken up by the fixed & auto sized rows.
c. Setting all the Heights to * for a set of rew definitions will ensure that the rows are given equal Heights from that which is remaining after the fixed and auto sized grids have be assigned spaces or heights.
Same is the case for a Column and its Width.

Conclusion:

These were just the basics of the Grid control but the most important and elementary chunks when it comes to developing UI in Silverlight. Do remind me if i have missed any points which can be added here, that you come across. I have only covered the basics and will deal with the other features and uses in another post. I do hope that this helped you a little.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1369599/pic.jpeg http://posterous.com/users/hcGXxsTkwP6SS Vijay Krishna Palepu vpalepu Vijay Krishna Palepu