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

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in z Silverlight Archives. Bookmark the permalink.

One Response to Creating a Project from xaml and xaml.cs files

Comments are closed.