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:
What is it? You can easily enable a visual Framerate counter inside your Silverlight Application. The end result looks like the image below:
Why do I care? This should be used for profiling/performance testing of your Silverlight Application before you move it into production status.
How do I do it: Simply add the following two lines inside your div tags:
<param name="enableFramerateCounter" value="True" />
<param name="enableGPUAcceleration" value="True" />
So your final div tag should look like the following (I’ve included the body tag):
<!--<span class="code-comment"> Runtime errors from Silverlight will be displayed here.
This will contain debugging information and should be removed or hidden
when debugging is completed --></span>
What is it? You can clear isolated storage on your Silverlight application from the browser instead of through code. A surprising amount of people are not aware of this.
Why do I care? It’s easy to access and do without writing any code. It also can be easily accomplished by an end user.
How do I do it: Simply right-click your Silverlight Application and select Silverlight then click on the Application Storage tab. At the bottom of the tab, you can either delete an individual sites isolated storage or all sites.
What is it? Inserting a line break inside of a TextBlock/TextBox.
Why do I care? You will need to insert a linebreak into a TextBlock or Textbox at some point...
How do I do it: You can do it inside of XAML or in code behind by reviewing the samples below:
Here is an example using XAML:
<TextBlock FontSize="24" Height="71" HorizontalAlignment="Left" Margin="12,12,0,0"
Name="textBlock1" VerticalAlignment="Top" Width="229" >
Hello, my name is <LineBreak/> Michael
You can also use any hexadecimal encoded value to represent a literal.
<TextBlock Text="Hello, my name is
Michael" FontSize="24" Height="71"
HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBlock1"
VerticalAlignment="Top" Width="229" />
Another way is at runtime with the following code snippet: Do not use '@' in front of the string definition, it will not work!
private void button1_Click(object sender, RoutedEventArgs e)
textBlock1.Text = "Line1\r\nLine2";
What is it? Embedding fonts (not included with Silverlight) into your Silverlight Application.
Why do I care? You will have a customer at some point that wants a custom font. Blend 4 makes this very easy, just be sure you have the copyright to distribute the font.
How do I do it: We are going to do it in Blend 4, because it is so easy. You can accomplish this in a matter of seconds and Blend does all the dirty work.
Let’s get started with a basic Silverlight project (refer to the numbers on each image).
- This is a standard Silverlight Project file structure.
- This is a TextBlock that we wish to change the font for.
- This is where we are going to select a font to embed. Go ahead and select a font and put a check in the “Embed” underneath the font.
You will notice a couple of things just happened.
- A new folder called Fonts was added and the actual Font TTF was placed inside of this directory.
- A preview of the new Font.
- The new Font selected with the “Embed” checkbox checked.
After looking at the code behind, you will see the following XAML. Pay attention to the FontFamily. That was easy.
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Height="80" Margin="22,30,0,0"
Text="This is an example of a new font. " Width="235"
What is it? You can check and see if a user is connected to the internet and handle accordingly.
Why do I care? If a user has lost connection to the internet, your application need to alert the user and save the current state.
How do I do it: We are going to add an Eclipse and textbox to our main page and follow up with some C Sharp code-behind. Note: In order to do this, you may have to disable and enable your network connection for testing. You can do this by going to Network and Internet and Network Connection and selecting disable as shown below:
Just change your XAML to the following:
<Ellipse Height="65" Name="ellipse1" Stroke="Black" StrokeThickness="3" Width="59" />
<TextBlock FontSize="24" Height="65" Name="textBlock1" Width="323" />
And your C# code-behind to the following:
NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(
void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
textBlock1.Text = "Network State: Connected";
ellipse1.Fill = new SolidColorBrush(Colors.Green);
textBlock1.Text = "Network State: Disconnected";
ellipse1.Fill = new SolidColorBrush(Colors.Red);
You will get the following screens when you have a valid network connected.
You will get the following screens when you have disconnected from the network.
That is it for Part 1, just 5 Tips/Tricks for a Silverlight Developer. Hopefully, you will subscribe to my blog for the last three parts.
Subscribe to my feed