question

DaljitSingh-1549 avatar image
0 Votes"
DaljitSingh-1549 asked ·

Best practices for splitting UWP XAML code into multiples files

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?

windows-uwpwindows-uwp-xaml
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, is the answer below useful to you? If you have other questions, please feel free to ask

0 Votes 0 ·
RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ivanich avatar image
0 Votes"
Ivanich answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.