Part 3 of 4 : Tips/Tricks for Silverlight Developers.
- by mbcrump
Part 1 | Part 2 | Part 3 | Part 4 I wanted to create a series of blog post 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: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;
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.
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 its 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.
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.
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();
}
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.
Subscribe to my feed
CodeProject