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 31 May 2008 09:12 PM by jesseliberty

Comments

# Dew Drop - June 1, 2008 | Alvin Ashcraft's Morning Dew said on 01 June, 2008 09:59 AM

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

# june 1 | Wonderful Article said on 01 June, 2008 06:22 PM

Pingback from  june 1  | Wonderful Article

# june 1 | Wonderful Article said on 01 June, 2008 06:22 PM

Pingback from  june 1  | Wonderful Article

# Wounded_Ego said on 02 June, 2008 02:35 PM

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

Bill Ross aka WoundedEgo.com

# Jesse Liberty - Silverlight Geek said on 06 June, 2008 11:45 AM

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

# Microsoft Weblogs said on 06 June, 2008 12:25 PM

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

# Launch Your Project » Blog Archive » In The News: New Project said on 23 June, 2008 05:52 PM

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

Search

Go

This Blog

News

     
     
    What's New In Silverlight 3
    A Frequently Updated WikiDoc .
    Last Update: July 10
     
    AgOpenSource
    From Design to implementation
     
    Better Videos
    Diary of an experiment in extreme post-production
     
    Quick Bits
    Too big for Twitter, Too Small for the blog
     
    New to Silverlight?
    Click here to get started.

Syndication