Jesse Liberty - Silverlight Geek

By, For and About Silverlight Developers

Creating a Project from xaml and xaml.cs files

I posted the code for the PageSwitcher app described in a previous blog post, but to save space and to make the download faster, I didn't include anything but the code (no solution or project files). A reader wrote asking how to create a project and it is a more than fair question as the answer is not obvious until you've done it a couple times, so let's walk through that example.

When you download the code, you'll receive a zip file named  PageSwitcher.zip. Unzip that and you'll have a folder named PageSwitcher,

PageSwitcherFiles

 

Open a new Visual Studio project and to make this example as clear as possible, let's name it something else (myPageSwitcher) and locate it in a different directory.

Open Page.xaml in your new project and note the name of the project (MyPageSwitcher)

<UserControl x:Class="MyPageSwitcher.Page"

Open Page.xaml.cs and note the namespace

namespace MyPageSwitcher

This is the information you need to hold on to for the rest of this exercise.

Ready To Go

There are many ways to do this, but the easiest is to delete Page.xaml, Page.xaml.cs and App.xaml and App.xaml.cs from your new project. (Don't panic!)

Next, right-click on the project and choose Add->Existing items and navigate to the downloaded files and add them all. They are now in your new project.

Click on all 4 xaml files and change the name of the project in the x:Class tag.

Click on all 4 .cs files and change the name of the namespace (ignore the smart tag) If you want to get rid of the smart tag, use Build->Clean. Build->Rebuild Solution.

You're all set

Here is your new PageSwitcher.xaml

<UserControl x:Class="MyPageSwitcher.PageSwitcher"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">

</UserControl>

 

And here is your new PageSwitcher.xaml.cs

using System.Windows.Controls;

namespace MyPageSwitcher
{
    public partial class PageSwitcher : UserControl
    {
        public PageSwitcher()
        {
            InitializeComponent();
            if (this.Content == null)
            {
                this.Content = new Page();
            }
        }
        public void Navigate(UserControl nextPage)
        {
            this.Content = nextPage;
        }
    }
}

 

Don't forget, once your code compiles, you can right click on the using statements and choose Organize Usings -> Remove Unused Using  which greatly cleans up your code.

RemoveUnusedUsing

Comments

Dew Drop - June 1, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - June 1, 2008 | Alvin Ashcraft's Morning Dew

# June 1, 2008 9:59 AM

june 1 | Wonderful Article said:

Pingback from  june 1  | Wonderful Article

# June 1, 2008 6:22 PM

june 1 | Wonderful Article said:

Pingback from  june 1  | Wonderful Article

# June 1, 2008 6:22 PM

Wounded_Ego said:

Wow, I never would have guessed that, or even figured that out! Thanks so much.

Bill Ross aka WoundedEgo.com

# June 2, 2008 2:35 PM

Jesse Liberty - Silverlight Geek said:

Beta 2 includes a wealth of new controls including the Popup (that is new, right? It’s not that I just

# June 6, 2008 11:45 AM

Microsoft Weblogs said:

Beta 2 includes a wealth of new controls including the Popup (that is new, right? It’s not that I just

# June 6, 2008 12:25 PM

Launch Your Project » Blog Archive » In The News: New Project said:

Pingback from  Launch Your Project  &raquo; Blog Archive   &raquo; In The News: New Project

# June 23, 2008 5:52 PM