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:

Calculate you project health from 7 questions

February 16, 2008 Shahed 1 comment

David posted this great list of 7 questions. Each of them are most essential for any project. I learned some of them from my experience in the hard way. Here is the checklist that every project manager should check:

1. Are you using a version control tool?

2. Do the people on your team know how to use version control to ‘time travel‘ through project versions?

3. Do you have good test coverage?

4. Can you build the project, run the tests, and update project documentation with one command?

5. Does the team get an automated email within 15 minutes of an accidental broken build?

6. Does that email contain the changeset from version control that broke the build?

7. Does your team have to work late nights whenever you have to release your project into production?

If you answered ‘no‘ to any of the first 6 questions, changes are you answered ‘yes‘ to the seventh one.

Read his post here.

Categories: Project Management

Error messages when trying to install IIS components on Windows XP

February 16, 2008 Shahed 1 comment

I was getting the following error message when tried to install IIS componentes:

“Setup cannot copy the file staxmem.dl_”

It was showing the error even after providing proper location of the file in WindowsXP installation CD. Finally, I found a solution in microsoft support site. According to the site, it could cause if the secedit.sdb in %windir%\security\database\ got damaged. To fix the secedit.sdb, do the following steps:

1. Make a backup copy of the secedit.sdb file.
2. Open a command prompt by clicking Start, Run, and then typing “cmd” and pressing Enter.
3. Type or copy and paste the following command
esentutl /p %windir%\security\database\secedit.sdb
and press Enter. Click “OK” when presented with “Repair” warning. Wait for repair to finish.

In my case, I installed a Virtual CD Rom software and seems to me that it caused the damage in secedit.sdb file.

Categories: IIS Tags:

How to reset MySql admin password

February 16, 2008 Shahed Leave a comment

Being a full time .net developer, I do most of my database related development in SQL Server. I recently moved to WordPress which use MySql as database. When I was setting up my new blog, I found that MySQL is already installed there but the password is not mentioned anywhere in the hosting docs. Now, in SQL Server by default the windows authentication is enabled. So you can easily login and change the password for sa.

But, for MySQL I found this interesting way:

  1. Go to Administrative tools -> Services and stop the ‘MySql’ service
  2. From command prompt, run Mysql\bin\mysqld-nt.exe” -P3306 –skip-grant-tables
  3. Open another command prompt and run mysql(\Mysql\bin\mysql.exe” -P3306 mysql)
  4. Now you can update mysql.user table to change the password for admin.
    For ex: update mysql.user set password=password(’new_password’) where user=’admin’
  5. Finally, you can close the prompts and kill the mysqld-nt.exe process from task manager and restart the service.
Categories: Database, MySQL Tags:

IIS Error “Unexpected error 0×8ffe2740 occurred”

February 16, 2008 Shahed Leave a comment

I have been getting the following error when trying to start IIS from my XP machine:

“Unexpected error 0×8ffe2740 occurred“

You can get more information in MS KB @ http://support.microsoft.com/?id=816944

Finally, I discovered that its caused by Skype. It listens to port 80 for incoming call. Now, most of the time IIS starts before Skype starts. So IIS works but if you restart IIS or try to start IIS after Skype is started then you will get this error message.

There are 2 possible solutions:

1. You can change your IIS settings to run in different port (say 81)

2. You can change your Skype settings
Skype File-> Options -> Connection
Uncheck Use Port 80 as an alternative for incoming connections.

Restart default website.

Categories: IIS Tags: