在(主要)无窗口 WPF 应用程序中将代码放在哪里?
因此,我计划编写一个主要是无窗口应用程序的应用程序。 将有用于用户交互的窗口,但仅应用户的请求。 否则,应用程序位于系统托盘中并运行计时器,每分钟左右执行一些逻辑。 我的问题是这样的:据我所知,我会将所有相关逻辑放在 App.xaml.cs 中(启动计时器,在每个时间间隔执行逻辑),但出于某种原因,这对我来说似乎是错误的。
该应用程序将有一个系统托盘图标,用户可以与之交互以获取详细信息、调整设置等,但除此之外,该应用程序只是坐在那里做它该做的事情。 将所有代码放在 App.xaml 类的代码隐藏中是否合适? 我不知道为什么,但似乎不应该,应该还有别的地方,但我实在想不出什么。
我以前从未编写过这样的应用程序,因此我很无知。 任何建议将不胜感激。 谢谢。
詹姆士
So I'm planning on writing an application that is largely a windowless application. There will be windows for user interaction, but only at the request of the user. Otherwise the application sits in the system tray and runs a timer, executing some logic every minute or so. My question is this: As far as I can tell, I would put all the related logic in App.xaml.cs (starting the timer, executing the logic at each interval), but for some reason this seems wrong to me.
The app will have a system tray icon that users can interact with to get details, adjust settings, etc., but otherwise the app just sits there and does what it does. Is it appropriate to put all the code in the code-behind for the App.xaml class? I don't know why, but it seems as if I shouldn't, and there should be somewhere else, but I can't really think of anything.
I've never written an app like this before, hence my ignorance. Any advice would be greatly appreciated. Thanks.
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使对于大多数交互是通过窗口完成的应用程序,将所有代码放在代码后面通常也是一个坏主意。 交互通常是在后面的代码中启动事件处理程序,但您可以将代码放在您自己创建的类中。
对于大多数时间不显示用户界面的应用程序也是如此。 大多数操作将从 App.xaml.cs 启动,但这并不意味着所有代码都必须驻留在其中。 例如,您可以将计时器封装在它们自己的类中,这些类可以启动其他代码来完成工作。 按照职责划分代码,窗口类执行 UI 操作,域逻辑进入其他文件等。这将使您能够创建更易于维护的应用程序。
Even with applications where most interaction is done through windows it's usually a bad idea to put all the code in the code behind. Interactions are often initiated eventhandlers in the code behind but you can put your code in classes you create yourself.
The same goes for applications that do not show a user interface most of the time. Most of the actions will be initiated from the App.xaml.cs but that doesn't mean all the code has to live there. You can encapsulate timers in their own classes that can kick off other code to do work for example. Divide your code up along lines of responsibilities, a window class does UI stuff, domain logic goes into other files etc. That will enable you to create more maintainable applications.
听起来代码不属于那里,最多只是启动计时器的调用。
听起来非常适合您的问题的是 MV-VM(模型 - 视图 - ViewModel)模式。 正如您所指出的,它也会“感觉”比将逻辑附加到后面的代码更正确。 使用 MVVM,您可以将稀疏的 UI 分离到视图中,并且您的代码可以单独存在于模型和视图模型中。
我建议使用工具包,因为它还包含良好的概述文档和示例,您可以在创建自己的解决方案时消化。 Laurent Bugnion 还发布了一个 MVVM 入门工具包,您可以使用它来入门。
另外,这里有一些好东西可以帮助您开始实际设置控件系统托盘。
It doesn't sound like the code belongs there, and at most just a call to start the timers.
What does sound like a perfect fit for your issues, is the M-V-VM (Model - View - ViewModel) pattern. As you noteded, it also will 'feel' more correct then attaching logic to your code behind. Using MVVM you can separate your sparse UI into a View, and your code can exist separately in the Model and ViewModel.
I would recomend using the toolkit here, as it also contains good overview documents and a sample you can digest as you create your own solution. Laurent Bugnion has also released a MVVM starter toolkit that you could use to get started.
Also, here is some good stuff to get you started on actually setting up your controls in the system tray.