locked
How to divide MainPage.xaml.cs file

    Question

  • The content of MainPage.xaml.cs is too larger.

    I'm going to divide the MainPage.xaml.cs into MainPage.xaml.cs and MainPage.xaml.A.cs.

    I create a xxx.cs file, and it's content as follows:

    public sealed partial class MainPage : Page
    {
        private void DoSomething()
        {
        }
    }
    However,  the compiler error occurs. can not find the method of DoSomething().


    How to do it?

    Wednesday, March 25, 2015 12:42 PM

Answers

  • Hi oneonce,

    You haven't provided enough information for us to say exactly what is going wrong. If you can share a minimal sample which demonstrates the problem then we can provide more direct help.

    Beyond that, make sure your second MainPageA.cs file is in the same namespace and is included in the project and it should just work:

    namespace MyApp
    {
        partial class MainPage
        {
            private void DoSomething();
        }
    }

    • Marked as answer by oneonce Thursday, March 26, 2015 12:37 AM
    Wednesday, March 25, 2015 6:38 PM
    Owner

All replies

  • Are you talking about class inheritance ?

        public class MainPageBase : Page
        {
            public void DoIt()
            {
            }
        }
    
        public class SpecificMainPage : MainPageBase
        {
            public SpecificMainPage()
            {
                InitializeComponent();
                DoIt();
            }
        }


    Wednesday, March 25, 2015 2:34 PM
  • Are you talking about class inheritance ?

        public class MainPageBase : Page
        {
            public void DoIt()
            {
            }
        }
    
        public class SpecificMainPage : MainPageBase
        {
            public SpecificMainPage()
            {
                InitializeComponent();
                DoIt();
            }
        }



    I sorry, my english is poor!

    MainPage.xaml.cs has much methods, I want copy some into the ABC.cs file, but the ABC.cs only has a class of MainPage.

    Wednesday, March 25, 2015 3:07 PM
  • You can define the class partial, like so:

        public partial class test
        {
            public int a;
        }
    
        public partial class test
        {
            public int b;
        }
    
    
    public class testclass
    {
       public test te=new test(); 
    }


    Wednesday, March 25, 2015 3:13 PM
  • Hi,

    Sounds to me like you want to move more code into the other class. For your code, Maybe you could't comprehend the partial class. You can reference the posted thread before. I think this article is what you need.

    Wednesday, March 25, 2015 3:48 PM
  • Hi,

    Sounds to me like you want to move more code into the other class. For your code, Maybe you could't comprehend the partial class. You can reference the posted thread before. I think this article is what you need.

    thanks for your reply.

    I want to move more code into other file, but the file's class also is MainPage. It's partial class of MainPage

    I'm sorry, my english is poor,  i may not be clearly described!


    • Edited by oneonce Wednesday, March 25, 2015 5:27 PM
    Wednesday, March 25, 2015 5:18 PM
  • Hi oneonce,

    You haven't provided enough information for us to say exactly what is going wrong. If you can share a minimal sample which demonstrates the problem then we can provide more direct help.

    Beyond that, make sure your second MainPageA.cs file is in the same namespace and is included in the project and it should just work:

    namespace MyApp
    {
        partial class MainPage
        {
            private void DoSomething();
        }
    }

    • Marked as answer by oneonce Thursday, March 26, 2015 12:37 AM
    Wednesday, March 25, 2015 6:38 PM
    Owner