Best practices for splitting UWP XAML code into multiples files

Daljit Singh 261 Reputation points
2020-04-19T22:45:44.143+00:00

I have been developing an XAML app for a while now, but I feel like my xaml files were too big so I started splitting them into multiple files. My strategy was to use the UserControl subclasses and create multiple controls that I would use inside other files. So for example I have my main page and inside that I had a ListView with some logic, so what I did was to create a new UserControl class called MyListView where I defined the custom logic for the list view and its UI datatemplates.
This works just fine, however reading the documentation and around the internet, I feel like I am abusing the concept of a UserControl which is something that is meant for a control used multiple times inside an app. In my case for example, I only use my "custom" listview only once but I am using something like this:

public sealed partial class MyListView : UserControl
    {
        // Custom code here
    }

just so that I could put some of the code from my main page inside MyListView.xaml out of convenience. So my question would be: is it fine to use UserControl in the way I am using it? If not, what is an alternative way that would allow me to split my application into multiple files? Furthermore, I heard that UserControl has a performance cost, is that true and what is the extent of this cost?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Richard Zhang-MSFT 6,936 Reputation points
    2020-04-20T05:43:19.92+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    The document clearly defines the purpose of UserControl:

    Provides the base class for defining a new control that encapsulates related existing controls and provides its own logic.

    I understand your needs. If a XAML file contains too much code, it will be a challenge for subsequent maintenance and updates. It is a good idea to split the page and divide the page into several modules. And of course, you could use UserControl to do it. You could also try other ways like using Resource Dictionary for styles and templates.

    For the second question, there is no official document mentions about the performance loss when using UserControl. There should be no problem using it.

    Thanks.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ivanich 306 Reputation points
    2020-04-20T11:36:21.697+00:00

    This works just fine, however reading the documentation and around the internet, I feel like I am abusing the concept of a UserControl which is something that is meant for a control used multiple times inside an app.

    You are right, in WPF, Silverlight and UWP, using UserControl to split layout and code into multiple files usually is considered bad practice. There are better tools for that: styles, templates and resource dictionaries.

    You can try to start with moving existing data templates and styles into resource dictionaries. Usually this solves the problem for 99% of your pages. In addition, almost any piece of layout can be replaced with ContentControl and templates in the ResourceDictionary.

    UserControl is last option in the list of your tools, and normally you don't have any custom controls in the project at all. Certainly, this will work, but it is intended for rare cases when you need some control and framework does not provide it out of the box. It wasn't designed to work like UserControl from WebForms.

    0 comments No comments