Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #87 – How to Change the Startup Class

When a Silverlight application is launched the entry point is a class that inherits from System.Windows.Application. By default, when you create a new Silverlight application project, this would be your App class which gets defined in App.xaml.cs. You can, however, change which startup class you want to use. In fact, if you do not plan to use XAML at all you can reduce the size of your application by deleting the Page and App classes including the XAML and code behind files.

To accomplish this follow these steps:

Step 1. Create a new Silverlight application project adding a new ASP.net web project to host the Silverlight app.

Step 2. Delete App.xaml and Page.xaml (the CS files will be deleted also).

Step 3. Create a new class and call it StartupTest.cs

Step 4. Modify your new class to inherit from Application as seen here:

public class StartupTest : Application
{
    public StartupTest()
    {
    
    }
}

Step 5. Right click on your Silverlight application project in the Solution Explorer of VS. From the context menu choose Properties. This will bring up the Properties dialog. Click on the Silverlight tab at the top-left and change the Startup object to be your class as seen circled here:

image

Step 6. Go back to your StartupTest class. Add an event to monitor for when the application has started. You don’t want to do anything until after this event has started.

public class StartupTest : Application
{
    public StartupTest()
    {
        this.Startup += new StartupEventHandler(StartupTest_Startup);
    }
 
 
    void StartupTest_Startup(object sender, StartupEventArgs e)
    {
    }        
}

Step 7. The Application.RootVisual is root pointer to the main application UI. You will need to create this object to point an object such as a Grid or Canvas control. Once created, you can add additional controls to the children as you see fit. In the example below, I have created a Canvas as the root and configured its background color to be black, its width to be 800 and height to be 600. Also, I have added a Textbox to the Canvas control.

void StartupTest_Startup(object sender, StartupEventArgs e)
{
    this.RootVisual = new Canvas();
 
    ((Canvas)this.RootVisual).Width = 800;
    ((Canvas)this.RootVisual).Height = 600;
    ((Canvas)this.RootVisual).Background = new SolidColorBrush(Colors.Black);
 
    TextBlock tb = new TextBlock();
    tb.Foreground = new SolidColorBrush(Colors.White);
    tb.FontSize = 20;
    tb.Text = "NO XAML NEEDED!!!";
 
    ((Canvas)this.RootVisual).Children.Add(tb);
}        

Step 8. Run the application and you will see the following image rendered in your browser:

image

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# January 23, 2009 7:21 PM

Microsoft Weblogs said:

When a Silverlight application is launched the entry point is a class that inherits from System.Windows

# January 23, 2009 7:25 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# January 25, 2009 7:47 AM

vitor_canova said:

Why did you fill Canvas properties after set to the RootVisual?

You can't do like above?

var MyCanvas = new Canvas();

MyCanvas.Width = 100;

MyCanvas.Height = 100;

this.RootVisual = MyCanvas;

There is performance difference?

# January 25, 2009 1:47 PM

Community Blogs said:

In this issue: Grant Archibald, Rob Houweling, Andrej Tozon, Dean Chalk, Ken Cox, Daniel Crenna, Bryant

# January 25, 2009 7:10 PM

Brandon Truong said:

# January 25, 2009 7:23 PM

How to Change the Startup Class « Brandontruong’s Weblog said:

Pingback from  How to Change the Startup Class « Brandontruong’s Weblog

# January 25, 2009 7:24 PM

How to Change the Startup Class « Brandontruong’s Weblog said:

Pingback from  How to Change the Startup Class « Brandontruong’s Weblog

# January 25, 2009 7:24 PM

mike.snow said:

vitor_canova - Your way works as well, no diff unless you need to keep that canvas pointer around

# January 25, 2009 11:18 PM

vitor_canova said:

Ok Mike, thank's.

# January 26, 2009 7:38 PM

Programming with Silverlight, WPF & .NET » Tip of The Day #84 - #89 How to …. said:

Pingback from  Programming with Silverlight, WPF & .NET » Tip of The Day #84 - #89 How to ….

# January 29, 2009 2:27 AM

Visual Web Developer Team Blog said:

Most Recent Posts: Tip #93 - Reading XML with Silverlight Tip #92 - How to Load Images from a Stream

# February 10, 2009 6:25 PM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:35 PM