WPF 桌面应用程序中的登录窗口和主应用程序窗口使用什么模式?

发布于 2024-09-19 20:54:11 字数 817 浏览 2 评论 0原文

首先,我们是C#、WPF、桌面应用程序(可以是.NET 3.5)。

我需要在应用程序启动时显示登录窗口,如果登录成功,隐藏登录窗口并显示主应用程序窗口。此外,当主窗口关闭时,应该会再次看到登录窗口。如果登录窗口关闭,应用程序将退出。

增加难度的奖励:当选中“记住用户”选项时,将跳过登录窗口并自动登录用户,并立即显示主窗口。 (请不要考虑选项是如何存储的,假设您只是知道它并且变量中有值)。

应用程序起始点是什么(意味着哪个窗口被认为位于 app.xaml StartupUri 属性中)?

您将如何解决架构问题?是使用 App_Startup 事件并从该点显示一些窗口的最佳方法吗?

您会使用什么类型的 App.ShutdownMode?那如何运作呢?您可以使用“OneLastWindowClose”、“OnMainWindowClose”或“OnExplicitShutdown”。你的选择是什么?为什么?

一般来说,我感兴趣的是“什么调用什么,什么是应用程序‘root’,什么调用关闭应用程序”。

我已经有了一个可以解决我的问题的解决方案,但我很感兴趣,如果你们中有人遇到过这个问题,并且是如何解决的?我很乐意分享我的方法,但我不想首先限制您的想法。

感谢您的贡献。

PS:我使用的是 MVVM 框架,这使得它有点混乱,但重点仍然存在。我也使用 Ninject 作为 IoC,但这个问题当然仍然存在。我没有提及这些信息是为了让问题尽可能清楚。我还必须处理异常(登录期间与服务器或数据库的通信可能会失败),并且为了使其真正有趣,我必须显示交互式“splashwindow”,指示应用程序的功能(加载、与服务器通信、自动登录)。但目前请远离这些要求,我们可以稍后再讨论。

First of all, we are in C#, WPF, desktop application (can be .NET 3.5).

I need to show login window when application starts, if login succeeds, hide login window and show main app window. In addition, when the main window is closed, one should see login window again. If login window is closed, application quits.

Bonus to make it harder: when "Remember user" option checked, login window is skipped and user is logged in automatically, showing the main window instatly. (please do not think about how the option is stored, assume you just know it and have the value in variable).

What is meant to be the application startpoint (means which window is considered to be in app.xaml StartupUri property)?

How would you solve architecture? Is the best way to use App_Startup event and show some window from that point?

What type of App.ShutdownMode would you use? And how would that work? You can use "OneLastWindowClose", "OnMainWindowClose" or "OnExplicitShutdown". What is your choice and why?

In general, I am interested in "what calls what, what is application 'root', what invokes closing the application".

I already have a solution for my problem that works, but I am interested, if any of you have met this problem and how have you solved it? I will gladly share my approach, but I don't want to limit your ideas in the first place.

Thank you for your contribution.

P.S.: I am using MVVM framework, that makes it just a little more messy, but the point stays. I also use Ninject as IoC, but this issue still of course stays on. I did not mention these information to make the question as clear as possible. I also have to handle exceptions (communication with server or db during the login may fail), and to make it really interesting, I must show interactive 'splashwindow' indicating what application does (loading, communication with server, autologin). But please stay away from these requirements at the moment, we can discuss them later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-09-26 20:54:11

我将根据我的理解重复您的问题,您可以纠正我遗漏的地方。另外,我对整个 MV-VM 还很陌生,所以请对我的建议持保留态度。我知道如何学习的最好方法就是抛弃我的想法并纠正它们。

你想了解一些事情
1)逻辑应该存在于哪里来决定在哪里显示登录或主窗口
2) 使用哪种关机模式
3) 什么是应用程序根

1)
我相信显示登录或自动登录的决定是业务逻辑,因此应该存在于 MV-VM 框架的模型中。一旦实现了该逻辑,视图就可以通过查询模型来显示所需的任何窗口。

我假设您的应用程序有一个 App.xaml 和 App.cs 文件,它们在执行应用程序时运行。您可以重写 App.cs 中的 OnStartup 并根据业务逻辑的结果(通过使用模型中的某些对象了解到)显示所需的窗口。

2)
对于关闭模型,我可能会选择 OnLastWindowClose,但我不知道您的应用程序正在使用多少个窗口。我假设只有您提到的两个(登录和主)。

3)
什么是应用程序根?我认为您的模型实际上是应用程序的根,因为它包含所有重要的内容(视图是模型的用户界面,视图模型是视图的状态)。因此,当谈到应用程序的根源或本质是什么时,我认为它是模型中的某些对象集。

什么叫什么?这完全取决于您想要实现的目标。一般来说,我避免让视图模型了解有关视图的任何信息。视图和视图模型都可以知道模型。在某些情况下,您需要使用 Ninject 作为服务管理器,以便将视图注入到视图模型中(而不强制视图模型依赖于视图)。


一种完全不同的方法是完全避免登录窗口并在主窗口中显示登录方法。您仍然需要一些模型对象来告诉您何时显示登录提示,但您不必担心处理窗口。老实说,我可能会走那条路。 WPF 为我们提供了许多有趣且性感的方法来完成该任务。

I'm going to repeat your questions as I understand them, you can correct me where I am missing something. Also, I'm rather new to the whole M-V-VM thing, so take my advice with a grain of salt. The best way I know how to learn is to throw out my ideas and have them corrected.

You want to know things
1) Where should the logic exist to decide where to show the login or main window
2) Which shutdown mode to use
3) What is application root

1)
I believe the decision to show a login or auto login is business logic, thus should live in the Model of your M-V-VM framework. Once that logic is implemented, the view can display whichever window is required by querying the Model.

I assume your application has an App.xaml and App.cs file which runs when the application is executed. You can override OnStartup within App.cs and display whichever window is necessary, based on the results of the business logic (which are learned from using some object in your Model).

2)
For shutdown model, I'd probably go with OnLastWindowClose, but I have no idea how many windows your application is using. I assume only the two you mentioned (login and main).

3)
What is application root? I would argue that your Model is really the application root, in that it holds all of the important stuff (view is user interface to the model, view model is state for the view). So, when it comes to what is the root or essence of your application, I would argue it is some set of objects in your model.

What calls what? That will all depend on what you're trying to accomplish. In general, I avoid having the view model know anything about the view. Both view and view model can be aware of the model. In certain cases, you'll want to make use of Ninject as a service manager in order to inject a view into the view model (without forcing the view model to depend on the view).


An entirely different approach would be to avoid a log in window altogether and display a login method in the main window. You'll still need some Model object to tell you when to show the login prompt, but you won't have to worry about juggling windows. Honestly, I'd probably go that route. WPF provides us with a lot of interesting and sexy ways to accomplish that task.

亢潮 2024-09-26 20:54:11

Application 对象是任何 WPF 项目的根。您可以处理 App_Startup 事件,这是正确的 - 当然,这取决于您在启动时需要执行的操作。

根据您是否需要更复杂的导航框架,或者应用程序将永远局限于您所描述的场景,您可以采取多种方式。

我在我处理的应用程序中所做的是创建一个 Navigator 对象,该对象处理不同窗口之间和窗口内部的导航,然后我只需调用 Navigator.Navigate(new MyViewModel(), NavMode.ReplaceWindow);< /code> 关闭前一个“主”窗口(如果有打开的)并显示一个新窗口,将内容设置为我的 ViewModel 实例。然后使用正确的模板显示 ViewModel。我还可以使用 NavMode.ReplaceUserControl 替换当前“主”窗口的内容,或使用 NavMode.OpenModalWindow 显示模式对话框。我的 ShutdownMode 设置为 OnLastWindowClose,因为如果我想关闭,无论打开的窗口如何,我总是可以调用 App.Shutdown()。

当然,当您更换窗口时,您需要确保在关闭旧窗口之前实例化(不是显示,只是实例化)新窗口,以避免因为没有更多窗口而导致应用程序关闭。

您可以做类似的事情,但这只是一个想法,它是专门根据我的要求量身定制的。

The Application object is the root of any WPF project. You are correct that you can handle the App_Startup event - depending on what you need to do at startup, of course.

Depending on whether you need a more complex navigation framework or the application will be forever limited to the scenario you described, you can go a multitude of ways from there.

What I'm doing in the applications I work on is I create a Navigator object that handles navigation both between different windows and inside windows, then I simply call Navigator.Navigate(new MyViewModel(), NavMode.ReplaceWindow); which closes the previous 'main' window (if any is open) and displays a new one, setting the contents to the instance of my ViewModel. The ViewModel is then displayed using the correct templates. I can also use NavMode.ReplaceUserControl which replaces the contents of the current 'main' window, or NavMode.OpenModalWindow which displays a modal dialog. My ShutdownMode is set to OnLastWindowClose, since I can always call App.Shutdown() if I want to close regardless of the open windows.

Of course, when you replace windows you need to make sure to instantiate (not show, just instantiate) the new window before closing the old one, to avoid application shutdown because you have no more windows.

You could do something similar, but it's just an idea and it's tailored specifically for my requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文