Wednesday, April 30, 2008

Color / SolidColorBrush object from color name in silverlight

The situation lead to this post was one of my friend's query.He just wants to set Background of something with a Color name in hand.
Basically string "Green" should be converted into Color object.
It seems like a silly thing as we have enough experience in .net.(I assume that persons wont jump into silverlight without basic skills in .net or jscript).But this becomes complicated when we are not able find out the method 'Color.FromName()'.
We were not able to find out a direct method which meets his requirement.But at last we arrived on the following.

private void Button_Click(object sender, RoutedEventArgs e) { 
string s = "<solidcolorbrush> xmlns='http://schemas.microsoft.com/client/2007'";

s+=" xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'" ;

s+=" Color='Green'/>";

SolidColorBrush brush =(SolidColorBrush) XamlReader.Load(s);

LayoutRoot.Background = brush;

}


It is nothing but creating a SolidColorBrush object from xaml where he can insert the color name in string.

Friday, April 11, 2008

Styling and Templating in Silverlight 2.0

There is no easy method to edit Styles or Templates of Silverlight 2.0 controls eventhough there is Style and Templating support.The actual problem is we can't find out the default Style or Template in ordre to edit.In WPF applications it's possible by opeing Application in Expression Blend.But we should alter the templates in order to get full strength Silverlight .ie the GREAT next generation look and FEEL.

Here you can found one tool which displays default style and template of Silverlight controls.just copy the style from the tool then alter and apply to your controls and have the power of Templating and Styling.
Hope in future versions of Blend there would be support to edit templates as we are doing for WPF application.Anyway thanks to Delay for providing such a tool.

The default styles are available in MSDN also.

Wednesday, April 9, 2008

Multiline TextBox in Silverlight 2.0

Its set of workarounds only.There is no support in silverlight 2.0 to have a multiline textbox.But we should accomplish as we might be in deadline :-).It leads to workarounds.
What I did ,was to set AcceptsReturn property of TextBox.Now it will accept Returns ie EnterKey and create one more line by increasing its' Height.
Now the next problem came.ScrollBars..
I tried setting ScrollViewer.VerticalScrollBarVisibility="Visible".But it gave me runtime error.(Dont know why VS2008 showed intellisense to add ScrollViewer.VerticalScrollBarVisibility="Visible").
Again workaround.Put this TextBox in a ScrollViewer.
Finally it looks like




<ScrollViewer Height="100" Width="100" HorizontalScrollBarVisibility="Visible">

<TextBox Text="Javascripting Silverlight 2.0"

AcceptsReturn="True"

/>

ScrollViewer>



Sample located here

Tuesday, April 8, 2008

Javascripting Silverlight 2.0

As the heading implies ,here I am going to explain about "Using Javascript with Silverlight 2.0".

Importance
Silverlight 2.0 has got so many exciting features/controls like Calendar,MultiScaleImage etc along with c# support.But what can a javascript developer do?
He can still code in javascript itself and make use of these new things.

Here goes some guidelines to start javascripting in Silverlight 2.0
  1. Since we don't have a built-in Visual studio template for creating Silverlight2.0 application using Javascript ,we have to use the available Template of either C# or VB.Net
  2. Getting reference to Plugin and other objects
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/SilverlightApplication2.xap" Version="2.0" Width="100%" Height="100%" OnPluginLoaded="plloaded" />


plloaded is a javascript function where we are going to code rest.
3 . Subscribing to events

<script language="javascript">
   1: function grd_MouseLeftButtonDown(sender,args){
   2:     alert("Clicked on " + sender.name);
   3: }
   4: function plloaded(sender,th)
   5: {
   6:     var i=sender.get_element();
   7:     var grd=i.Content.findName("LayoutRoot");//LayoutRoot is the Grid Present in Page.Xaml 
   8:     grd.AddEventListener("MouseLeftButtonDown", "grd_MouseLeftButtonDown")
   9: }
</script>


Its done this will invoke the grd_MouseLeftButtonDown function on mouse click of Grid present in Xaml.

A sample which implements this method is located here

Friday, April 4, 2008

Silverlight 2.0 crossdomain programming

'Crossdomain' is a common word in Silverlight programming from the days of wpf/e itself.Actually what is 'crossdomain' ?
It's nothing but trying to access something located at different domains other than the silverlight application's domain.

These calls are blocked in silverlight.But we can't create a complete application without making these calls.for eg: if we need to access a database ,we should make these types of calls since Silverlight dont have direct database suppport.In such situations we do the following.

Create a proxy in the same domain of Silverlight application and route calls through that proxy.Proxy usually be a webservice or WCF service which dont have the crossdomain limitation.

There are so many things which we should take care of when using these methods.Some links are given below which helps you to improve your application's communication skill.

http://blogs.msdn.com/silverlightws/archive/2008/03/30/some-tips-on-cross-domain-calls.aspx
http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2008/03/19/WCF-Integration-in-Silverlight-2-Beta-1.aspx