R – the easiest way to break up large XAML files in the application

user-controlswpfxaml

I recently discovered that I could make use of user controls to reduce the size of my main application's .xaml file. I am new to WPF and realized that my app's XAML is getting very long, very fast and is cumbersome to scroll through.

Are user controls the best way to address this issue (i.e. have lots of user controls and their templates in a separate control library)?

How do you manage your XAML?

Thanks in advance!

Best Answer

You're correct that splitting things into seperate controls can help reduce the size of individual xaml files. The other way to reduce the size of the files is to make use of ResourceDictionaries. When you split your Styles, Templates, and Resources into seperate ResourceDictionaries it can greatly reduce the size of your window's and control's xaml files. Once stuff is split up, you can make use of the MergedDictionaries feature to access the styles and templates from wherever you need them. If a specific resource is used throughout multiple windows and controls, you can also merge it's ResourceDictionary into the App.xaml resources, thus allowing it to be used from any location in the solution.

Personally, I like to keep each XAML file to around ~300 or less lines, after that point I see if there's a better way I should have it organized. Here's some additional info and tips on how to keep your Resources organized.

Related Topic