Click here to Skip to main content
11,493,937 members (62,146 online)
Click here to Skip to main content

Tagged as

Part 3 of 4 : Tips/Tricks for Silverlight Developers

, 13 Dec 2010 CPOL 6K 3
Rate this:
Please Sign up or sign in to vote.
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:

<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…

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.

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.

<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>
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:

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:

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

<iframe marginwidth="0" marginheight="0" src=""http://ads.geekswithblogs.net/a.aspx?ZoneID=5&Task=Get&PageID=31016&SiteID=1"" frameborder="0" width="1" scrolling="no" height="1" />

License

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

Share

About the Author

mbcrump
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.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sandeep Mewara24-Dec-10 20:59
mentorSandeep Mewara24-Dec-10 20:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 13 Dec 2010
Article Copyright 2010 by mbcrump
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid