Page view counter

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

Published Saturday, May 31, 2008 9:12 PM by jesseliberty

Comments

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

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

# june 1 | Wonderful Article

Pingback from  june 1  | Wonderful Article

Sunday, June 01, 2008 6:22 PM by june 1 | Wonderful Article

# june 1 | Wonderful Article

Pingback from  june 1  | Wonderful Article

Sunday, June 01, 2008 6:22 PM by june 1 | Wonderful Article

# re: Creating a Project from xaml and xaml.cs files

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

Bill Ross aka WoundedEgo.com

Monday, June 02, 2008 2:35 PM by Wounded_Ego

# Popup Control

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

Friday, June 06, 2008 11:45 AM by Jesse Liberty - Silverlight Geek

# Popup Control

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

Friday, June 06, 2008 12:25 PM by Microsoft Weblogs

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

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