Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#

Part 3 of 4 : Tips/Tricks for Silverlight Developers

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
13 Dec 2010CPOL3 min read 10.2K   3   1
Part 3 of 4 : Tips/Tricks for Silverlight Developers

Part 1 | Part 2 | Part 3 | Part 4

I wanted to create a series of blog posts that gets right to the point and is aimed specifically at Silverlight Developers. The most important things I want this series to answer is:

  • What is it?
  • Why do I care?
  • How do I do it?

I hope that you enjoy this series. Let’s get started:

Tip/Trick #11)

What is it? Underline Text in a TextBlock.

Why do I care? I've seen people do some crazy things to get underlined text in a Silverlight application. In case you didn’t know there is a property for that.

How do I do it: On a TextBlock, you have a property called TextDecorations. You can easily set this property in XAML or CodeBehind with the following snippet:

XML
<UserControl x:Class="SilverlightApplication19.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Name="txtTB" Text="MichaelCrump.NET" 
        TextDecorations="Underline" />
    </Grid>
</UserControl>

or you can do it in CodeBehind…

C#
txtTB.TextDecorations = TextDecorations.Underline;

image

Tip/Trick #12)

What is it? Get the browser information from a Silverlight Application.

Why do I care? This will allow you to program around certain browser conditions that otherwise may not be aware of.

How do I do it: It is very easy to extract Browser Information out a Silverlight Application by using the BrowserInformation class.

image

You can copy/paste this code snippet to have access to all of them.

C#
string strBrowserName = HtmlPage.BrowserInformation.Name;
string strBrowserMinorVersion = 
	HtmlPage.BrowserInformation.BrowserVersion.Minor.ToString();
string strIsCookiesEnabled = HtmlPage.BrowserInformation.CookiesEnabled.ToString();
string strPlatform = HtmlPage.BrowserInformation.Platform;
string strProductName = HtmlPage.BrowserInformation.ProductName;
string strProductVersion = HtmlPage.BrowserInformation.ProductVersion;
string strUserAgent = HtmlPage.BrowserInformation.UserAgent;
string strBrowserVersion = HtmlPage.BrowserInformation.BrowserVersion.ToString();
string strBrowserMajorVersion = 
	HtmlPage.BrowserInformation.BrowserVersion.Major.ToString();

Tip/Trick #13)

What is it? Always check the minRuntimeVersion after creating a new Silverlight application.

Why do I care? Whenever you create a new Silverlight application and host it inside of an ASP.NET website, you will notice Visual Studio generates some code for you as shown below. The minRuntimeVersion value is set by the SDK installed on your system. Be careful, if you are playing with beta’s like “Lightswitch” because you will have a higher version of the SDK installed. So when you create a new Silverlight 4 project and deploy it your customers, they will get a prompt telling them they need to upgrade Silverlight. They also will not be able to upgrade to your version because it's not released to the public yet.

How do I do it: Open up the .aspx or .html file Visual Studio generated and look for the line below. Make sure it matches whatever version you are actually targeting.

image

Tip/Trick #14)

What is it? The VisualTreeHelper class provides useful methods for involving nodes in a visual tree.

Why do I care? It’s nice to have the ability to “walk” a visual tree or to get the rendered elements of a ListBox. I have it very useful for debugging my Silverlight application.

How do I do it: Many examples exist on the web, but say that you have a huge Silverlight application and want to find the parent object of a control. In the code snippet below, we would get 3 MessageBoxes with (StackPanel first, Grid second and UserControl third). This is a tiny application, but imagine how helpful this would be on a large project.

XML
<UserControl x:Class="SilverlightApplication18.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <Button Content="Button" Height="23" Name="button1" 
		VerticalAlignment="Top" Width="75" Click="button1_Click" />
        </StackPanel>
    </Grid>
</UserControl>
C#
private void button1_Click(object sender, RoutedEventArgs e)
{
    DependencyObject obj = button1;

    while ((obj = VisualTreeHelper.GetParent(obj)) != null)
    {
        MessageBox.Show(obj.GetType().ToString());
    }
}

Tip/Trick #15)

What is it? Add ChildWindows to your Silverlight Application.

Why do I care? ChildWindows are a great way to direct a user attention to a particular part of your application. This could be used when saving or entering data.

How do I do it: Right click your Silverlight Application and click Add then New Item. Select Silverlight Child Window as shown below:

SNAGHTML24f4fd05

Add an event and call the ChildWindow with the following snippet below:

C#
private void button1_Click(object sender, RoutedEventArgs e)
{
    ChildWindow1 cw = new ChildWindow1();
    cw.Show();
}

image

Your main application can still process information but this screen forces the user to select an action before proceeding.

The code behind of the ChildWindow will look like the following:

C#
namespace SilverlightApplication18
{
    public partial class ChildWindow1 : ChildWindow
    {
        public ChildWindow1()
        {
            InitializeComponent();
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            //TODO: Add logic to save what the user entered. 
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

Thanks for reading and please come back for Part 4.

alt Subscribe to my feed

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Telerik
United States United States
Michael Crump is a Silverlight MVP and MCPD that has been involved with computers in one way or another for as long as he can remember, but started professionally in 2002. After spending years working as a systems administrator/tech support analyst, Michael branched out and started developing internal utilities that automated repetitive tasks and freed up full-time employees. From there, he was offered a job working at McKesson corporation and has been working with some form of .NET and VB/C# since 2003.

He has worked at Fortune 500 companies where he gained experience in embedded systems design and software development to systems administration and database programming, and everything in between.

His primary focus right now is developing healthcare software solutions using Microsoft .NET technologies. He prefers building infrastructure components, reusable shared libraries and helping companies define, develop and automate process standards and guidelines.

You can read his blog at: MichaelCrump.net or follow him on Twitter at @mbcrump.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sandeep Mewara24-Dec-10 19:59
mveSandeep Mewara24-Dec-10 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.