locked
Is it possible to use 2 .cs files to one .xaml file? RRS feed

  • Question

  • Hi,

    In my project, I have a MainPage.xaml file and a MainPage.xaml.cs file. In this .cs file, I can write code against the UI controls, like buttons, datagrids, radiobuttons etc. I also uses classes but those cant interact with the UI controls.

    The thing is that my .cs file is 125,000 lines now and I get outOfMemoryExceptions sometimes.

    I simply wonder if it is possible to have 2 .cs files that are connected to MainPage.xaml like MainPage.xaml.cs and MainPage2.xaml.cs ?

    Friday, April 20, 2012 7:51 AM

Answers

  • This is how you do it with partial classes:

    suppose your xaml looks like this,

    <UserControl x:Class="OneXAMLmultiCS.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <CheckBox Name="chkCheckBox"/> <Button Name="btnButton" /> </StackPanel> </Grid> </UserControl>

    and the code behind looks like this:

    namespace OneXAMLmultiCS
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                btnButton.Content = "Button content Set by original mainpage";
                btnButton.Click += new RoutedEventHandler(btnButton_Click);
            }
    
            void btnButton_Click(object sender, RoutedEventArgs e)
            {
                chkCheckBox.IsChecked = true;
                chkCheckBox.Content = "set by mainpage 2";
            }
        }
    }

    Suppose you want to move your eventhandler btnButton_Click to a separate file, follow these steps.
    1. Locate in solution explorer hierarchy, where Mainpage.xaml exists. (MainPage.xaml.cs looks one level below the xaml, but actually it builds into a single class on compile).
    2. At the same level as MainPage.xaml, create a class file with some other name, say MainPage_2.xaml.cs.
    3. Copy paste the 'using' statements, the 'namespace' declaration and the 'class' definition from mainpage.xaml.cs to this new class.
    4. Cut the code you want to move from MainPage.xaml.cs to the newly created class as follows:
    namespace OneXAMLmultiCS
    {
        public partial class MainPage : UserControl
        {
            void btnButton_Click(object sender, RoutedEventArgs e)
            {
                chkCheckBox.IsChecked = true;
                chkCheckBox.Content = "set by mainpage 2";
            }
        }
    }


    4. Notice that the namespace name and class name are the same, only the name of the file is different. Build the solution. 


    Tuesday, May 1, 2012 11:11 AM

All replies

  • Yes you can. You just have to use the partial keyword on you class and you can creat as many files as you want. However, I would strongly recommend to read something about object oriented programming and how to structure your project.


    partial class MainaPage: Page

     

    Friday, April 20, 2012 11:23 AM
  • Thanks,

    Yes I did hear something about the partial class one time but didn´t get exactly. Yes I know I haven´t structred the project very good. Though I wonder if it is a real problem if the project actually runs and works as it does. Perheps the application "eats" more memory than it should etc?

    Just to be sure now, I have to ask again to catch this. As now my project is called "myProject" and in my MainPage.xaml.cs file, it looks like below. How will it look like in MainPage2.xaml.cs when I create that file?

    namespace myProject
    {
        public partial class MainPage : UserControl
        {
            //All code here
        }
    }
    
    Saturday, April 21, 2012 7:17 PM
  • Hi Silvers,

    To achieve the goal, you may try the suggestion in this thread, using two different ViewModels or just using inheritance.

    Best Regards,

    Monday, April 23, 2012 9:27 PM
    Moderator
  • Thanks, I was looking at the thread but there was no examples of how to achieve this.

    The information that seems interesting was this one. I wonder how we can achive this in practic and how to set this up?

    >>  just use inheritance. Write two different implementations and have them each inherit from your XAML's code-behind.

    Thursday, April 26, 2012 10:08 AM
  • Hi,

    I still wonder how to solve this exactly. I am stuck on the below as it seems to be a good solution but are not sure of how set it up in code exactly?

    "just use inheritance. Write two different implementations and have them each inherit from your XAML's code-behind."

    Monday, April 30, 2012 4:10 PM
  • This is how you do it with partial classes:

    suppose your xaml looks like this,

    <UserControl x:Class="OneXAMLmultiCS.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <CheckBox Name="chkCheckBox"/> <Button Name="btnButton" /> </StackPanel> </Grid> </UserControl>

    and the code behind looks like this:

    namespace OneXAMLmultiCS
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                btnButton.Content = "Button content Set by original mainpage";
                btnButton.Click += new RoutedEventHandler(btnButton_Click);
            }
    
            void btnButton_Click(object sender, RoutedEventArgs e)
            {
                chkCheckBox.IsChecked = true;
                chkCheckBox.Content = "set by mainpage 2";
            }
        }
    }

    Suppose you want to move your eventhandler btnButton_Click to a separate file, follow these steps.
    1. Locate in solution explorer hierarchy, where Mainpage.xaml exists. (MainPage.xaml.cs looks one level below the xaml, but actually it builds into a single class on compile).
    2. At the same level as MainPage.xaml, create a class file with some other name, say MainPage_2.xaml.cs.
    3. Copy paste the 'using' statements, the 'namespace' declaration and the 'class' definition from mainpage.xaml.cs to this new class.
    4. Cut the code you want to move from MainPage.xaml.cs to the newly created class as follows:
    namespace OneXAMLmultiCS
    {
        public partial class MainPage : UserControl
        {
            void btnButton_Click(object sender, RoutedEventArgs e)
            {
                chkCheckBox.IsChecked = true;
                chkCheckBox.Content = "set by mainpage 2";
            }
        }
    }


    4. Notice that the namespace name and class name are the same, only the name of the file is different. Build the solution. 


    Tuesday, May 1, 2012 11:11 AM
  • Thank you very much. That is working out great and will solve a lot of problems

    Thank you!

    Tuesday, May 1, 2012 12:09 PM