I'm about to create WPF application. So far at uni the only way we have ever done GUIs is to have one main window with one code-behind file for handling its button clicks etc..
我即将创建WPF应用程序。到目前为止,在uni中我们完成GUI的唯一方法是使用一个主窗口和一个代码隐藏文件来处理其按钮点击等。
My issue is that as the application grows, the GUI grows, and the size of the code behind file can get really out of hand!
我的问题是,随着应用程序的增长,GUI会增长,文件后面的代码大小可能会失控!
I have identified about 15 main use cases for my system (example: enter details, view details, etc...). I am creating a main window (size: 480x320) that consists of 15 seperate screens (one for each use case). This could be achieved with a centered and stretched TabControl, with 15 TabItem's. Or more likely it could just be a bunch of layered containers, one on top of the other (only one visible at a time).
我已经为我的系统确定了大约15个主要用例(例如:输入详细信息,查看详细信息等...)。我正在创建一个主窗口(大小:480x320),由15个单独的屏幕组成(每个用例一个)。这可以通过一个居中和拉伸的TabControl来实现,它有15个TabItem。或者更可能的是它可能只是一堆分层容器,一个在另一个之上(一次只能看到一个)。
The point is, with 15 seperate screens, my code-behind file will become enormous (not to mention the xaml file!): Juggling between states - making 14 collapsed/hidden and making one visible, Handling the controls for 15 different screens.
关键是,有15个单独的屏幕,我的代码隐藏文件将变得巨大(更不用说xaml文件!):在状态之间玩杂耍 - 使14折叠/隐藏并使一个可见,处理15个不同屏幕的控件。
Is there a way to having 15 seperate forms, each with its own code-behind file, instead of 15 TabItems on the one form, and then having one main engine creating and scrapping them as needed? Ofcourse, it should appear as though it is one form, not 15 pop ups.
有没有办法拥有15个单独的表单,每个表单都有自己的代码隐藏文件,而不是一个表单上的15个TabItems,然后根据需要创建一个主引擎并将其废弃?当然,它应该看起来好像是一种形式,而不是15种弹出窗口。
How do i deal with this? How would you deal with the issue of xaml and code-behind files that are thousands of lines long?
我该如何处理?您将如何处理长达数千行的xaml和代码隐藏文件的问题?
6 个解决方案
#1
5
Your instincts are good: you don't want to put everything in one Window. You would be far better off putting each of the 15 "screens" in its own XAML file, as either user controls, or pages.
你的直觉很好:你不想把所有东西都放在一个窗口里。将每个15“屏幕”放在自己的XAML文件中,作为用户控件或页面,你会好得多。
If Web-browser-style navigation would make sense for your application, then look at the Page class. If you set your Application's StartupUri to point to a Page (instead of a Window), then you'll automatically get a window with Back and Forward buttons, and you can use Hyperlinks (set the NavigateUri property to point to another Page) or the methods of NavigationService to navigate to new pages.
如果Web浏览器样式的导航对您的应用程序有意义,那么请查看Page类。如果您将应用程序的StartupUri设置为指向页面(而不是窗口),那么您将自动获得一个带有后退和前进按钮的窗口,您可以使用超链接(将NavigateUri属性设置为指向另一个页面)或NavigationService导航到新页面的方法。
If you don't want the Back and Forward buttons, then put each "screen" in its own UserControl, and add some minimal logic to the main Window to show and hide them. Or if you're using MVVM, you could set up some magic where you just change your Window's DataContext (or better yet, a property on your application-level ViewModel) and it automatically loads and shows the correct UserControl (look into DataTemplates, or watch the video below).
如果您不想使用“后退”和“前进”按钮,则将每个“屏幕”放在其自己的UserControl中,并将一些最小逻辑添加到主窗口以显示和隐藏它们。或者如果您正在使用MVVM,您可以设置一些魔法,只需更改Window的DataContext(或者更好的是,应用程序级ViewModel上的属性),它会自动加载并显示正确的UserControl(查看DataTemplates,或者观看下面的视频)。
I would also strongly recommend that you use MVVM to try to write as little codebehind as possible (ideally none at all -- not always achievable, but you'll learn a lot by trying). That makes your XAML tons easier to refactor. If you later decide that one of your Grids has way too much stuff on it, you can just cut and paste it into a new UserControl, without needing to spend tons of time disentangling all the codebehind.
我还强烈建议您使用MVVM尝试尽可能少地编写代码(理想情况下根本没有 - 不总是可以实现,但是通过尝试你会学到很多东西)。这使您的XAML吨更容易重构。如果你后来决定你的一个网格上有太多的东西,你可以将它剪切并粘贴到一个新的UserControl中,而不需要花费大量的时间来解开所有的代码隐藏。
Since it sounds like you're not familiar with the MVVM pattern, this video might go over your head, but I can't help recommending the MIX2010 talk "Build Your Own MVVM Framework". It's an eye-opener on what MVVM can do, and has some solid ideas on how to manage navigation between different UserControls. (It also has a link to an intro-to-MVVM talk.)
由于听起来你不熟悉MVVM模式,这个视频可能会超出你的想象,但我不禁推荐MIX2010的演讲“构建你自己的MVVM框架”。这是MVVM可以做什么的大开眼界,并且对如何管理不同UserControl之间的导航有一些坚实的想法。 (它还有一个指向MVVM简介的链接。)
#2
6
If you are about to start using WPF you should definitely have a look at the MVVM design pattern and commanding. You should also consider using dependency injection, via a IOC container, it is pretty easy to set up, this is a great tutorial.
如果您即将开始使用WPF,您一定要看看MVVM设计模式和命令。您还应该考虑使用依赖注入,通过IOC容器,它很容易设置,这是一个很棒的教程。
In your example, it would be possible to create a single view (user control) that appears on a relevant tab page, that view would have its own related view model class that provide the data and handle any interaction via commands. Each view model would be bound to its view via the IOC container, thus making it easier to change the behavior of the user interface.
在您的示例中,可以创建出现在相关选项卡页面上的单个视图(用户控件),该视图将具有自己的相关视图模型类,该类提供数据并通过命令处理任何交互。每个视图模型都将通过IOC容器绑定到其视图,从而更容易更改用户界面的行为。
#3
1
You should create each screen as UserControl and reference that in your MainWindow xaml.
您应该将每个屏幕创建为UserControl并在MainWindow xaml中引用它。
However, it seems like you have an enormous design flaw in your application, i.e. having your logic in the UI. The code-behind file is not there to drive your application or logic, it is there to drive your UI! You might want to have a look into the MVC pattern and Dependency Injection to solve this.
但是,您的应用程序中似乎存在巨大的设计缺陷,即在UI中具有逻辑。代码隐藏文件无法驱动您的应用程序或逻辑,它可以驱动您的UI!您可能希望了解MVC模式和依赖注入以解决此问题。
The Wpf Application Framework would be a start for that.
Wpf应用程序框架将是一个开始。
#4
0
The WPF Composite Application Guidance (or Prism) uses the concept of UI Composition to handle breaking the UI down to smaller classes.
WPF复合应用程序指南(或Prism)使用UI组合的概念来处理将UI分解为更小的类。
#5
-1
In terms of the code-behind files, you could use #region tags to allow you to collapse and expand the different screens... Although, splitting it out logically is a better approach if possible
就代码隐藏文件而言,您可以使用#region标签来折叠和扩展不同的屏幕......虽然如果可能的话,将逻辑分开是更好的方法
#6
-2
You could make your class partial
and split it up into separate files.
您可以使您的类部分并将其拆分为单独的文件。
#1
5
Your instincts are good: you don't want to put everything in one Window. You would be far better off putting each of the 15 "screens" in its own XAML file, as either user controls, or pages.
你的直觉很好:你不想把所有东西都放在一个窗口里。将每个15“屏幕”放在自己的XAML文件中,作为用户控件或页面,你会好得多。
If Web-browser-style navigation would make sense for your application, then look at the Page class. If you set your Application's StartupUri to point to a Page (instead of a Window), then you'll automatically get a window with Back and Forward buttons, and you can use Hyperlinks (set the NavigateUri property to point to another Page) or the methods of NavigationService to navigate to new pages.
如果Web浏览器样式的导航对您的应用程序有意义,那么请查看Page类。如果您将应用程序的StartupUri设置为指向页面(而不是窗口),那么您将自动获得一个带有后退和前进按钮的窗口,您可以使用超链接(将NavigateUri属性设置为指向另一个页面)或NavigationService导航到新页面的方法。
If you don't want the Back and Forward buttons, then put each "screen" in its own UserControl, and add some minimal logic to the main Window to show and hide them. Or if you're using MVVM, you could set up some magic where you just change your Window's DataContext (or better yet, a property on your application-level ViewModel) and it automatically loads and shows the correct UserControl (look into DataTemplates, or watch the video below).
如果您不想使用“后退”和“前进”按钮,则将每个“屏幕”放在其自己的UserControl中,并将一些最小逻辑添加到主窗口以显示和隐藏它们。或者如果您正在使用MVVM,您可以设置一些魔法,只需更改Window的DataContext(或者更好的是,应用程序级ViewModel上的属性),它会自动加载并显示正确的UserControl(查看DataTemplates,或者观看下面的视频)。
I would also strongly recommend that you use MVVM to try to write as little codebehind as possible (ideally none at all -- not always achievable, but you'll learn a lot by trying). That makes your XAML tons easier to refactor. If you later decide that one of your Grids has way too much stuff on it, you can just cut and paste it into a new UserControl, without needing to spend tons of time disentangling all the codebehind.
我还强烈建议您使用MVVM尝试尽可能少地编写代码(理想情况下根本没有 - 不总是可以实现,但是通过尝试你会学到很多东西)。这使您的XAML吨更容易重构。如果你后来决定你的一个网格上有太多的东西,你可以将它剪切并粘贴到一个新的UserControl中,而不需要花费大量的时间来解开所有的代码隐藏。
Since it sounds like you're not familiar with the MVVM pattern, this video might go over your head, but I can't help recommending the MIX2010 talk "Build Your Own MVVM Framework". It's an eye-opener on what MVVM can do, and has some solid ideas on how to manage navigation between different UserControls. (It also has a link to an intro-to-MVVM talk.)
由于听起来你不熟悉MVVM模式,这个视频可能会超出你的想象,但我不禁推荐MIX2010的演讲“构建你自己的MVVM框架”。这是MVVM可以做什么的大开眼界,并且对如何管理不同UserControl之间的导航有一些坚实的想法。 (它还有一个指向MVVM简介的链接。)
#2
6
If you are about to start using WPF you should definitely have a look at the MVVM design pattern and commanding. You should also consider using dependency injection, via a IOC container, it is pretty easy to set up, this is a great tutorial.
如果您即将开始使用WPF,您一定要看看MVVM设计模式和命令。您还应该考虑使用依赖注入,通过IOC容器,它很容易设置,这是一个很棒的教程。
In your example, it would be possible to create a single view (user control) that appears on a relevant tab page, that view would have its own related view model class that provide the data and handle any interaction via commands. Each view model would be bound to its view via the IOC container, thus making it easier to change the behavior of the user interface.
在您的示例中,可以创建出现在相关选项卡页面上的单个视图(用户控件),该视图将具有自己的相关视图模型类,该类提供数据并通过命令处理任何交互。每个视图模型都将通过IOC容器绑定到其视图,从而更容易更改用户界面的行为。
#3
1
You should create each screen as UserControl and reference that in your MainWindow xaml.
您应该将每个屏幕创建为UserControl并在MainWindow xaml中引用它。
However, it seems like you have an enormous design flaw in your application, i.e. having your logic in the UI. The code-behind file is not there to drive your application or logic, it is there to drive your UI! You might want to have a look into the MVC pattern and Dependency Injection to solve this.
但是,您的应用程序中似乎存在巨大的设计缺陷,即在UI中具有逻辑。代码隐藏文件无法驱动您的应用程序或逻辑,它可以驱动您的UI!您可能希望了解MVC模式和依赖注入以解决此问题。
The Wpf Application Framework would be a start for that.
Wpf应用程序框架将是一个开始。
#4
0
The WPF Composite Application Guidance (or Prism) uses the concept of UI Composition to handle breaking the UI down to smaller classes.
WPF复合应用程序指南(或Prism)使用UI组合的概念来处理将UI分解为更小的类。
#5
-1
In terms of the code-behind files, you could use #region tags to allow you to collapse and expand the different screens... Although, splitting it out logically is a better approach if possible
就代码隐藏文件而言,您可以使用#region标签来折叠和扩展不同的屏幕......虽然如果可能的话,将逻辑分开是更好的方法
#6
-2
You could make your class partial
and split it up into separate files.
您可以使您的类部分并将其拆分为单独的文件。