在我的应用程序中分解大型 XAML 文件的最简单方法是什么? [英] What is the easiest way to break up large XAML files in my application?

查看:37
本文介绍了在我的应用程序中分解大型 XAML 文件的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现我可以利用用户控件来减小主应用程序的 .xaml 文件的大小.我是 WPF 的新手,并意识到我的应用程序的 XAML 变得非常长、非常快并且滚动起来很麻烦.

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)?

您如何管理您的 XAML?

How do you manage your XAML?

提前致谢!

推荐答案

您说得对,将事物拆分为单独的控件有助于减小单个 xaml 文件的大小.减小文件大小的另一种方法是使用 资源字典.当您将样式、模板和资源拆分为单独的 ResourceDictionaries 时,它可以大大减少窗口和控件的 xaml 文件的大小.拆分内容后,您可以使用 MergedDictionaries 功能来从您需要的任何位置访问样式和模板.如果在多个窗口和控件中使用特定资源,您还可以将其 ResourceDictionary 合并到 App.xaml 资源中,从而允许从解决方案中的任何位置使用它.

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.

就我个人而言,我喜欢将每个 XAML 文件保持在大约 300 行或更少行,在那之后我看看是否有更好的方式来组织它.这里是一些附加信息和有关如何保持资源井井有条的提示.

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.

这篇关于在我的应用程序中分解大型 XAML 文件的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆