Using nested class to seperate control-generating methods from a Page class


Note that this is for the C# only approach when developing forms apps.

I have been thinking about how to best structure a pages code when building it entirely in C#. My first approach was to just place it in the Page class and that resulted in 20+ methods and each dedicated to generating and binding properties from the ViewModel. For me it just felt that the class got inflated and i wanted a way to separate the "UI methods" just like how using a XAML page separats the UI from the code-behind.

The issue with just extracting it to a new class is that it can be reached by other parts in the app so i settled with using a private nested class inside the page-class to handle all the controls that needed bo be generated. That way the Page class can focus on initializing and grabbing the ViewModel and set up the layout of the page and then fill it with all the controls from the nested class instead.

For me that makes the most sense but I still feel like it can be made better so I would like som feedback on this approach and other ways of doing this.


  • DavidDancyDavidDancy AUMember ✭✭✭✭
    edited December 2016

    What do you think about using a partial class approach? It would have to be a "convention" rather than something the compiler could enforce, but you could split your code into different files then, with one (or more) files dedicated to the ContentView and its children, and one (or more) dedicated to the Page (subclass) and bindings.

Sign In or Register to comment.