Part 4 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 #16)
What is it? Find out version information about Silverlight and which WebKit it is using by going to http://issilverlightinstalled.com/scriptverify/.
Why do I care? I’ve had those users that its just easier to give them a site and say copy/paste the line that says User Agent in order to troubleshoot a Silverlight problem. I’ve also been debugging my own Silverlight applications and needed an easy way to determine if the plugin is disabled or not.
How do I do it: Simply navigate to http://issilverlightinstalled.com/scriptverify/ and hit the Verify button. An example screenshot is located below:
Results from Chrome 7
Results from Internet Explorer 8 (With Silverlight Disabled)
Tip/Trick #17)
What is it? Use Lambdas whenever you can.
Why do I care? It is my personal opinion that code is easier to read using Lambdas after you get past the syntax.
How do I do it: For example: You may write code like the following:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Check and see if we have a newer .XAP file on the server
Application.Current.CheckAndDownloadUpdateAsync();
Application.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
}
void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
if (e.UpdateAvailable)
{
MessageBox.Show(
"An update has been installed. To see the updates please exit and restart the application");
}
}
To me this style forces me to look for the other Method to see what the code is actually doing. The style located below is much easier to read in my opinion and does the exact same thing.
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
//Check and see if we have a newer .XAP file on the server
Application.Current.CheckAndDownloadUpdateAsync();
Application.Current.CheckAndDownloadUpdateCompleted += (s, e) =>
{
if (e.UpdateAvailable)
{
MessageBox.Show(
"An update has been installed. To see the updates please exit and restart the application");
}
};
}
Tip/Trick #18)
What is it? Prevent development Web Service references from breaking when Visual Studio auto generates a new port number.
Why do I care? We have all been there, we are developing a Silverlight Application and all of a sudden our development web services break. We check and find out that the local port number that Visual Studio assigned has changed and now we need up to update all of our service references. We need a way to stop this.
How do I do it: This can actually be prevented with just a few mouse click. Right click on your web solution and goto properties. Click the tab that says, Web. You just need to click the radio button and specify a port number. Now you won’t be bothered with that anymore.
Tip/Trick #19)
What is it? You can disable the Close Button a ChildWindow.
Why do I care? I wouldn’t blog about it if I hadn’t seen it. Devs trying to override keystrokes to prevent users from closing a Child Window.
How do I do it: A property exist on the ChildWindow called “HasCloseButton”, you simply change that to false and your close button is gone. You can delete the “Cancel” button and add some logic to the OK button if you want the user to respond before proceeding.
Tip/Trick #20)
What is it? Cleanup your XAML.
Why do I care? By removing unneeded namespaces, not naming all of your controls and getting rid of designer markup you can improve code quality and readability.
How do I do it: (This is a 3 in one tip)
Remove unused Designer markup:
1)
Have you ever wondered what the following code snippet does?
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignWidth="640" d:DesignHeight="480"
This code is telling the designer to do something special with this page in “Design mode” Specifically the width and the height of the page. When its running in the browser it will not use this information and it is actually ignored by the XAML parser. In other words, if you don’t need it then delete it.
2)
If you are not using a namespace then remove it. In the code sample below, I am using Resharper which will tell me the ones that I’m not using by the grayed out line below. If you don’t have resharper you can look in your XAML and manually remove the unneeded namespaces.
3)
Don’t name an control unless you actually need to refer to it in procedural code. If you name a control you will take a slight performance hit that is totally unnecessary if its not being called.
<TextBlock Height="23" Text="TextBlock" />
That is the end of the series. I hope that you enjoyed it and please check out Part 1 | Part 2 | Part 3 if your hungry for more.
Subscribe to my feed
CodeProject