Windows窗体/ WPF太大,我怎么能拆呢了? [英] Windows form/WPF too large, how can i split it up?

查看:137
本文介绍了Windows窗体/ WPF太大,我怎么能拆呢了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建WPF应用程序。到目前为止,在大学我们曾经做过的GUI的唯一方法是有一个主窗口与一个代码隐藏文件处理的点击按钮等。

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..

我的问题是随着应用程序的增长,在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!

我已经确认了大约15个主要的用例为我系统(例如:输入详细信息,查看详细信息等)。我建立一个主窗口(大小:分辨率480x320),它由15个独立的屏幕(每个使用情况)。这可以用一个居中,拉伸TabControl的实现,有15 TabItem的公司。或者更可能它可能只是一串分层的容器,一个在另一个的顶部(仅一次一个可见)。

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独立的屏幕,我的代码隐藏文件将成为巨大的:国家之间的杂耍 - 使14崩溃/(更不用说XAML文件!)隐藏的,使一个可见的,处理了15个不同的屏幕控制。

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独立的形式,每个,而不是15的TabItems的一种形式,然后有一个主机创建和报废并根据需要? Ofcourse,它应该出现,就好像它是一种形式,而不是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.

我要如何面对呢?你会如何处理的是成千上万的排长队的XAML和代码隐藏文件的问题?

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文件中的15屏幕,因为无论是用户控件或页面。

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.

如果网络浏览器样式导航将是有意义的申请,然后看类。如果您设置应用程序的的StartupUri 指向页面(而不是窗口),那么你会自动获得与后退和前进按钮的窗口,你可以使用的超链接 S(设置的 NavigateUri 属性指向另一页)或的NavigationService 导航到新的页面。

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.

如果您不希望返回和前进按钮,然后把每一个屏幕,在自己的用户控件,并添加一些最小的逻辑到主窗口显示和隐藏它们。或者,如果你使用 MVVM ,您可以设置一些神奇的地方你只需要改变你的窗口的 DataContext的(或更好然而,在你的应用程序级的视图模型),它会自动加载和显示正确的用户控件的属性(看看的的DataTemplate S,或观看下面的视频)。

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

我也强烈建议您使用MVVM尝试写尽可能少的代码隐藏越好(最好根本不 - 并不总是可以实现的,但是你会试图学到很多)。这使得你的XAML的的更容易重构。如果您稍后决定您的网格中的一个上有太多的东西,你可以剪切并粘贴到一个新的用户控件,而无需花费解开所有的代码隐藏大量时间。

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模式,这个视频可能去在你的头上,但我不能帮推荐的MIX2010谈话的自创MVVM框架。这是什么MVVM可以做一个大开眼界,并且对如何管理不同的用户控件之间的导航一些坚实的想法。 (它也有一个链接到一个介绍到MVVM谈话。)

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

这篇关于Windows窗体/ WPF太大,我怎么能拆呢了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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