.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Silverlight Spy

Silverlight Spy allows you to investigate Silverlight application. Just enter the URL in the address bar and Silverlight Spy will automatically pick up all embedded Silverlight applications. The XAP is displayed in an explorer type tree view.

Some of the features:

1. View contents of the XAP.

2. View isolated storage that is used by the application.

3. View the styles of each control. This is great as you can cut and paste the style of any control.

4. Integration with Red Gate's Reflector (formerly Lutz's Reflector) provides the disassembled code in the XAP.

5. Built-in browser.

6. Event, HTTP, and performance monitoring while the application is running.

I'm impressed and I've only used it for a few hours now.

SilverlightSpy 

Guess the movie

He tasks me. He tasks me, and I shall have him!

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Tuesday, September 30, 2008 11:40 AM
Permalink | Comments (11) | Post RSSRSS comment feed

Does Silverlight force duplication of code? Part Two

In a previous post I mentioned that it is difficult to share code between the Silverlight client and server. The IDE does not allow a class library to be referenced from a Silverlight application. In addition, a Silverlight class library cannot be referenced from a non Silverlight project. This restriction makes sense since the Silverlight .NET Framework is a subset of the .NET Framework.

If you do need to share code then you can use the Visual Studio Add As Link feature where you add an existing item as a link, effectively using the same code. My recommendation is to create the code in a Silverlight class library and then add a regular class library and add the code from the Silverlight class library by using Add Existing Item - Add As Link.

Add as Link

Figure 1: Linking to an existing item.

 

Thanks to SLADAPTER for the response on Silverlight.net forums.

Guess the movie

What can you expect when you're on top? You know? It's like Napoleon. When he was the king, you know, people were just constantly trying to conquer him, you know, in the Roman Empire. So, it's history repeating itself all over again.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Thursday, September 25, 2008 4:15 AM
Permalink | Comments (16) | Post RSSRSS comment feed

Does Silverlight force duplication of code?

I've been playing around with Silverlight and how it works with other project types. So far it appears that regular class libraries cannot be referenced from within a Silverlight application. Give it a try, you should get the following error

Adding Class Library to Silverlight Project

Figure 1: Trying to add a class library to a Silverlight project

 

Likewise when you try to add a Silverlight class library to a web application project you get the following error

Adding Silverlight Class Library to Web Project  

Figure 2: Trying to add a Silverlight class library to a Web Application project

 

Why would you want to do this? Whenever you need to share code between the client and the server. Data Transfer Objects (DTO) come to mind. Although adding a service reference will generate the proxy code and the DTOs but sometimes you may what to forgo the auto-magically generated code by the IDE and code it yourself. Miguel Castro has a presentation on why you might want to do this.

What about validation code? There's a place where we would want the same code running on the client and the server.

I'll be posting this on Silverlight.net forums to see if there is some work around.

Please check this post for a work around.

Guess the movie

I know that you know that I know that you know that that is a dialouge between Confucius and Chuang Tzu.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Wednesday, September 24, 2008 4:45 PM
Permalink | Comments (6) | Post RSSRSS comment feed

Exposing Events in Custom Silverlight Controls

This is a fairly typical problem. You have created a Silverlight control that has built-in controls such as buttons. Each of these built-in controls has events that you want to expose to the page that is to consume the custom control. How do we go about accomplishing this you may ask? That is what this post is all about.

To begin let's create a simple Silverlight application. I'll be demonstrating using the VB.NET language, but feel free to use C# or any managed language you fell comfortable with. After you have created the Silverlight application the solution should look similar to Figure 1.

F1 Initial Solution

Figure 1: Initial Solution 

 

Next we will add a  New Project to the solution using the Silverlight Class Library project template. I chose to name it ExposedButton. By default a class is added to this project. Delete it and add a New Item using the Silverlight User Control template. Name this new item ExposedButton.xaml. You should now have a solution that is similar in structure to Figure 2.

F2 Solution with Silverlight Control

 Figure 2: Solution with the Silverlight User Control

 

For the purposes of this demo we will keep the XAML to a minimum. Add a button to the ExposedButton.xaml user control. Listing 1 shows the markup for the ExposedButton user control.

 

    1 <UserControl x:Class="ExposedButton.ExposedButton"

    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4    Width="400" Height="300">

    5 

    6     <Grid x:Name="LayoutRoot" Background="White">

    7         <Button x:Name="btnChangeColor"

    8                Content="Change Color"

    9                FontSize="16"

   10                Width="150" Height="50"/>

   11     </Grid>

   12 

   13 </UserControl>

Listing 1: The ExposedButton markup.

 

The next step is to expose an event in the code behind. There are many ways of accomplishing this, I've decide to implement a custom event. Listing 2 shows the code behind for this the ExposedButton user control.

 

    1 Partial Public Class ExposedButton

    2     Inherits UserControl

    3 

    4     Public Sub New

    5         InitializeComponent()

    6     End Sub

    7 

    8     Public WriteOnly Property BackColor() As Color

    9         Set(ByVal value As Color)

   10             LayoutRoot.Background = New SolidColorBrush(value)

   11         End Set

   12     End Property

   13 

   14     Custom Event ExposedButtonClick As RoutedEventHandler

   15         AddHandler(ByVal value As RoutedEventHandler)

   16             AddHandler btnChangeColor.Click, value

   17         End AddHandler

   18         RemoveHandler(ByVal value As RoutedEventHandler)

   19             RemoveHandler btnChangeColor.Click, value

   20         End RemoveHandler

   21         RaiseEvent(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)

   22         End RaiseEvent

   23     End Event

   24 

   25 End Class

Listing 2: The ExposedButton code behind

 

The code contains a custom event and write only property. The custom event simply takes a delegate and adds it to the underlying button click event. Later we will show how to attach a delegate to ExposedButtonClick event. The Property is used to change the background color. That's all for the user control. The next step is to use the ExposedButton control in a Silverlight application. Before proceeding make sure that you have recently built the entire solution.

In the ExposingEvents project, add a reference to the ExposedButton project. The next step is add the ExposedButton namespace to the Page.xaml file. As shown in Figure 3, our favourite friend intellisense will make it easy to add the namespace.

F3 Namespace Intellisense

Figure 3: Intellisense support for adding namespace

 

Now. as shown in Listing 3, add the ExposedButton user control the Page.xaml markup.

    1 <UserControl x:Class="ExposingEvents.Page"

    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4    xmlns:eb="clr-namespace:ExposedButton;assembly=ExposedButton"

    5    Width="400" Height="300">

    6     <Grid x:Name="LayoutRoot" Background="White">

    7         <eb:ExposedButton x:Name="ebtn"></eb:ExposedButton>

    8     </Grid>

    9 </UserControl>

Listing 3: Adding the ExposedButton user control to Page.xaml

 

We are now in a position to add the event handler code to the code behind. Again we will rely on intellisense to wire-up the click event to an event handler. Figure 4 shows the intellisense support for adding the event handler.

F4 Adding Click Event

Figure 4: Using intellisense to wire up the event handler.

 

You should now have markup that is similar to Listing 4.

   

    1 <UserControl x:Class="ExposingEvents.Page"

    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4    xmlns:eb="clr-namespace:ExposedButton;assembly=ExposedButton"

    5    Width="400" Height="300">

    6     <Grid x:Name="LayoutRoot" Background="White">

    7         <eb:ExposedButton x:Name="ebtn" 

    8            ExposedButtonClick="ebtn_ExposedButtonClick">

    9         </eb:ExposedButton>

   10     </Grid>

   11 </UserControl>

Listing 4: Final markup for Page.xaml

 

All that's left to do is to add some code in the code behind. The code is simple, just change the background color of the user control. Listing 5 contains the code behind.

    1 Partial Public Class Page

    2     Inherits UserControl

    3 

    4     Public Sub New()

    5         InitializeComponent()

    6     End Sub

    7 

    8     Private Sub ebtn_ExposedButtonClick( _

    9         ByVal sender As System.Object, _

   10         ByVal e As System.Windows.RoutedEventArgs)

   11         ebtn.BackColor = Colors.Red

   12     End Sub

   13 

   14 End Class

Listing 5: Code behind for Page.xaml

 

That's it run the application and click the button, the background color should change to red.

Guess the movie

Almost at the moment He died, I heard Him say, "Father, forgive them for they know not what they do."

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Friday, September 19, 2008 3:04 PM
Permalink | Comments (68) | Post RSSRSS comment feed

WCF: The Good the Bad and the Ugly

 

I make it a point to watch DNRTV every week. Carl Franklin the host of this excellent video cast program has a new presentation just about every week. The speakers are top notch. Last week a regular, Miguel Castro, gave a presentation on WCF. This was not your typical WCF talk with the usual demos where WCF services are created using the Visual Studio project templates. Instead, Miguel, created a complete WCF application client and host from scratch without the help of any project templates. In addition, the contract, implementation and host code were separated into individual assemblies. On the client side, a separate assembly was generated to house the proxies. The client application consumed the service by referencing this proxy assembly. This approach makes complete sense. If you are doing any work with WCF or plan to please make sure to catch this great presentation.

Miguel's presentation was done using the C# language and the code can be downloaded from the DNRTV site. I have duplicated the work in VB and if you are interested source can be download here.

Why the title you may ask? Well, Miguel's presentation first talked about the typical WCF presentation that uses the built-in Visual Studio project templates. He named the projects BadClient and BadService. He then went on to demo the 'Good' way of creating a WCF service and consuming it. Naturally, I thought of the great Sergio Leone classic. Hey, if you really want to get into the mood then drag the mouse over the poster to the left and sit back and enjoy.

Guess the movie (This one should be easy)

There are two kinds of people in the world, my friend. Those who have a rope around their neck and those who have the job of cutting.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: WCF
Posted by CynotWhyNot on Tuesday, September 16, 2008 12:20 PM
Permalink | Comments (75) | Post RSSRSS comment feed