Archive

Archive for the ‘Silverlight’ Category

Implementing your own base class for user controls in Silverlight 2

June 9, 2008 Shahed 7 comments

The objective is to create your own base class for user controls to implement application related features and at the same time also use the features provided by the Visual Studio (i.e. auto generate a partial class that initialize all UI elements). The process can be described best with three projects:

For the sake of simplicity I created a simple UserControlBase class extending from the UserControl class. This class can contain the common methods and properties as needed for your application. Here I have added some dummy methods and properties.

namespace BaseLibrary
{
public class UserControlBase : UserControl
{
public int Id { get; set; }
public void DoSomeThing()
{
//…
}
}
}

Then, lets create a TestControl class and a TestControl.xaml in the class library where we like the have the custom controls:

namespace CustomControls
{
public partial class TestControl : UserControlBase
{
public TestControl()
{
InitializeComponent();
}

}
}

Now, here is the trick. Look closely to the xaml. Instead of regular UserControl, we used our own base class. To do so, we also have to include the namespace.

UserControlBase x:Class=”MyControls.TestControl”
xmlns=”http://schemas.microsoft.com/client/2007″
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:bl=”clr-namespace:BaseLibrary;assembly=BaseLibrary”
Width=”150″ Height=”50″>

<TextBlock Text=”I am a test control”/>

</bl:UserControlBase>

In this way, Visual Studio also generates the partial class properly. But there is one side effect: the Visual Studio will not be able to show you the UI preview in designer. I haven’t found any work around yet.

Update:

I forgot to add the reference in AsseblyInfo.cs file of BaseLibrary project. Once you add the following reference the Visual Studio will render the UI properly. Thanks to Michael for pointout the issue.

[assembly: XmlnsDefinition("http://schemas.microsoft.com/client/2007", "BaseLibrary")]

I have also updated the sample code.
Download Sample Code

Categories: Silverlight Tags:

Hosting Silverlight 2.0 in IIS

April 2, 2008 Shahed Leave a comment

If you host Silverlight 2.0 application in IIS you might find that the silverlight object is not loading. If that’s the case, first thing you should check is the MIME type list. By default, the Silverlight package extension *.xap is not included.

To resolve that, go to IIS Manager, properties of your web site and check the “Http Headers” tab.

1

Then click the MIME Types button. You should see a list as bellow.

2

If the .xap extension is not in the list then click New… and add the following entry:

Extension: .xap
MIME type: application/x-silverlight-app

Now, check your site. You should get the silverlight objects properly.

kick it on DotNetKicks.com

Categories: IIS, Silverlight Tags:

Converting Silverlight 1.1 Alpha projects to Silverlight 2 Beta 1

March 6, 2008 Shahed Leave a comment

Just installed the runtime and Silverlight Tools for Visual Studio 2008. You can find the necessary tools here. The beta 1 now has a good set of controls including Data Grid, cool!

To run the Silverlight 1.1 Alpha projects in Beta 1, you need to manually modify few things.

First, if you try to open the old projects in VS 2008 after installing the latest bits, you will get the following error message:

“Unable to read the project file ‘… .csproj’. … The imported project “C:\Program Files\MSBuild\Microsoft\VisualStudio\v9.0\Silverlight\Microsoft.Silverlight.Csharp.targets” was not found.”

Just open the project file with notepad and replace the <import> node value.

Old: <Import Project=”$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\Silverlight\Microsoft.Silverlight.Csharp.targets” />

New: <Import Project=”$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.CSharp.targets” />

Save the project file and now you can load it in Visual Studio. Now that you have the projects opened, if you expand the Reference node you will see this:

image

You need to delete agclr, system.silverlight and system.Xml.core from the reference list. Then add the new assemblies: System.Windows, System.Windows.Browser, System.Windows.Controls and System.Windows.Controls.Extended.

image

Ok, lets do a rebuild now. Don’t worry if you get a long list or errors. Here is a quick list the things you may just do find and replace:

  • There is a PointCollection class now. So if you have List<Point> then you canĀ change it to PointCollection
  • For Keyboard events (i.e. KeyUp, KeyDown…) replace KeyboardEventArgs with KeyEventArgs
  • System.Windows.Browser.Serialization is moved, so you need to block //using System.Windows.Browser.Serialization
  • Previously, in xaml we used to write x:Class=”SilverlightApplication1.Page;assembly=ClientBin/… .dll”. Now we don’t need to mention the assembly part. Just write x:Class=”SilverlightApplication1.Page”
  • For mouse events (i.e. MouseDown, MouseUp…) replace the EventHandler with MouseEventHandler

Now, look into the web service. Lets assume you have a service named MyService and you have added the necessary web reference. The Silverlight 1.1 Alpha would generate a MyService class and to call any method asynchronously you would probably write something like this:

void Test()
{
MyService service = new MyService();
AsyncCallback callback = new AsyncCallback(FooCallback);
Core.BeginFoo(parameter, service);
}

FooCallback(IAsyncResult result)
{
MyService service = result.AsyncState as MyService;
service.EndSyncCommand(result);
}

In silverlight 2 beta 1, you need to do this slightly differently. It will generate a MySerciceSoapClient class. So, you may write something like this:

void Test()
{
MyServiceSoapClient service = new MyServiceSoapClient();

service.FooAsync();

service.FooCompleted += new EventHandler<FooCompletedEventArgs>(service_FooCompleted);
}

void service_FooCompleted(object sender, FooCompletedEventArgs e)
{


}

Ok, that’s all I had to do to make my project compile properly in beta 1. Please note that there may beĀ better ways to do this.

Categories: Silverlight Tags: