Posted in Silverlight

Silverlight 5.0 Released !!

Silverlight 5 is finally released!

In addition to the platform having a release, I was really pleased to see an update to the Silverlight Toolkit, which has been one of the most popular things almost every Silverlight developer/application uses.

I would love to explore this new set of tools and will post some sample codes as I build them..

Go download the stuff and start building great apps..

Have fun !!

Advertisements
Posted in C# 4.0, Silverlight

Dynamic Style/theme Change in Silverlight 4.0

In Silverlight application, we often need to change the theme or style dynamically. It is quite simple to do from code side. Here is an example how to do that.

I have two separate theme files, Theme1.xaml and Theme2.xaml defined as ResourceDictionary.

Theme1.xaml

  <ResourceDictionary  
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
  xmlns:sys="clr-namespace:System;assembly=mscorlib">       
  <!-- explicit style (has a key) -->
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">
        <Setter Property="Text" Value="SuperAwesome Co. LLC" />
        <Setter Property="Foreground" Value="Orange" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="15" />
 </Style>
 <!-- implicit style (no key - just a TargetType) -->
 <Style TargetType="Button">
     <Setter Property="Foreground" Value="Orange" />
     <Setter Property="Background" Value="Yellow" />
</Style>
</ResourceDictionary>

Theme2.xaml

<ResourceDictionary     
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     
   xmlns:sys="clr-namespace:System;assembly=mscorlib">     
   <!-- explicit style (has a key) -->     
  <Style x:Name="headerTextBlockStyle" TargetType="TextBlock">         
     <Setter Property="Text" Value="SomeOtherCompany Inc." />         
     <Setter Property="Foreground" Value="CornflowerBlue" />        
     <Setter Property="FontWeight" Value="Bold" />         
     <Setter Property="FontSize" Value="14" />     
  </Style>       
  <!-- implicit style (no key - just a TargetType) -->     
  <Style TargetType="Button">         
     <Setter Property="Foreground" Value="BlueViolet" />         
     <Setter Property="Background" Value="DarkOrchid" />     
  </Style> 
</ResourceDictionary>

Now I have added their reference in my App.xaml as below;

 <Application.Resources>         
    <ResourceDictionary>             
       <ResourceDictionary.MergedDictionaries>                 
         <ResourceDictionary Source="Themes/theme1.xaml"/>                 
         <ResourceDictionary Source="Themes/theme2.xaml"/>             
       <ResourceDictionary.MergedDictionaries>         
   </ResourceDictionary>    
</Application.Resources>

By default it will use Theme2.xaml as it is the last defined style. Now we’ll change the theme and use Theme1.xaml. Here is the code to change the theme dynamically from code behind.

  ResourceDictionary rd = new ResourceDictionary();
  //Load resource dictionary
  rd.Source = new Uri("/Themes/theme1.xaml", UriKind.RelativeOrAbsolute);
  //Clear previous styles if any...
  App.Current.Resources.MergedDictionaries.Clear();
  //Add the loaded resource dictionary to the application merged dictionaries
  App.Current.Resources.MergedDictionaries.Add(rd);

Hope this help some one 🙂

Posted in Microsoft, Silverlight

Send Email using Outlook from Silverlight 4.0 & COM

Today we’ll see one simple functionality of sending email from Silverlight application. I know many of you think that it is a very simple stuff.

Let’s do it using your Outlook application. To achive this we need to use a class called AutomationFactory which is available in System.Runtime.InteropServices.Automation.AutomationFactory.

Here is the code, which is very straight forward. But remember you must have to run the application out of browser to make this work!!

You need to add

using System.Runtime.InteropServices.Automation;
            var outlook = AutomationFactory.CreateObject("Outlook.Application");
            var olMail = outlook.CreateItem(0);
            olMail.To = txtTOEmailAddress.Text;
            olMail.Cc = txtCCEmailAddress.Text;
            olMail.Attachments.Add(@"C:\mypic.jpeg");
            olMail.Subject = txtsubject.Text;
            olMail.HTMLBody = txtmessage.Text;
            olMail.Display();
            olMail.Save();
            //You can send a mail by using Send() method.
            olMail.Send();

I hope this will help someone.

Posted in C# 4.0, Silverlight, WPF

Load Content of a Window object to another Control

Recently I have been working with WPF. I was developing a functionality of Child window and I need to load the content of existing windows to that child window control. I got the Child Window control from the CodePlex. I think many of you must be aware of it. There is a project called Extended WPF toolkit. You can find some nice control there. Here is the code to load the content of Window object to a user control (Child Window in my case).

Window2 win = new Window2();
Grid newContent = win.Content as Grid;
win.Content = null;
ChildWin.Content = newContent;

“Window2” is the name of my window that I wanted to open in child window. The root element in the Window2 is Grid, so I cast the content as Grid and assigned it to a local variable of type Grid.

Now, note the use of: win.Content = null;

To use the content of an object we need to disconnect the content from its parent, in this case it is the instance of Window2. This line will do the trick and allow you to use the content the way you like. I have put this content to my child window’s content and that is it.

I got what I want.

You can see above, I have display the content of Window2 to my Child Window control.

I hope this help to someone.

Posted in Silverlight

Lazy Initialization

Today I was searching for some tips to improve performance of my Silverlight application and I came across a feature called Lazy Initialization.

Continue reading “Lazy Initialization”