从 WPF 中的不同位置访问表单
仍在从 win 表单程序员那里习惯 WPF。我的应用程序中有多个可以从多个位置访问的表单,因此我需要保持表单“全局”,因为我不确定更好的术语。
例如,“详细信息”可以从“主菜单”打开,但也可以从“搜索”中的网格打开,我希望从搜索返回的详细信息显示在“详细信息”页面中,即使它是从主菜单预先打开。
我遇到了 Application.Current.Properties
并开始在其中存储一些表单,但感觉设置完全错误:
Vehicle vehicleForm = new Vehicle();
Application.Current.Properties["frmVehicle"] = vehicleForm;
然后访问它:
if (Application.Current.Properties["frmVehicle"] == null)
Application.Current.Properties["frmVehicle"] = new frmVehicle();
Vehicle vehicleFrm = (Vehicle)Application.Current.Properties["frmVehicle"];
vehicleFrm.Show();
vehicleFrm.Activate();
我刚刚发现了 Application.Properties。 Current.Windows
也让我有点困惑。
处理这样的表单最有效/行业标准的方法是什么?
Still getting used to WPF from a win forms programmer. I have multiple forms in an application that can be accessed from multiple locations, so I need to keep the forms "global" as I'm not sure of a better terminology.
For instance "Details" can be opened from a "Main Menu" but can also be opened from a grid in "Search", I'd like the details returned from the search to be displayed in the "Details" page even if it was pre-opened from the main menu.
I've come across Application.Current.Properties
and have started storing a few forms in it but it just feels plain wrong to set:
Vehicle vehicleForm = new Vehicle();
Application.Current.Properties["frmVehicle"] = vehicleForm;
And then to access it:
if (Application.Current.Properties["frmVehicle"] == null)
Application.Current.Properties["frmVehicle"] = new frmVehicle();
Vehicle vehicleFrm = (Vehicle)Application.Current.Properties["frmVehicle"];
vehicleFrm.Show();
vehicleFrm.Activate();
I have just discovered Application.Current.Windows
as well which has thrown me a little.
What is the most efficient/industry standard way of dealing with form like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是检查
Application.Current.Windows 是否
包含您的窗口的一个实例。如果是这样,那么你就给它焦点,如果不是,那么你就创建一个实例。I would just check whether
Application.Current.Windows
contains an instance of your window. If so then you give it focus, if not then you create an instance.我不确定我是否理解你如何正确打开窗户。但是,如果您只想在应用程序的整个运行时拥有一个窗口实例,则可以使用 单例模式。基本上,窗口类有一个静态属性来保存唯一的实例。
如果您不需要在窗口中保留任何状态,则可以在每次要显示它时创建它的新实例。
I'm not sure if I understand how are you opening the window correctly. But if all you want to do is to have one instance of the window through the whole run time of the application, you can use the Singleton pattern. Basically, the window class has a static property that holds the only instance.
If you don't need to keep any state in the window, you can just create new instance of it every time you want to show it.