使用WindowManager重新激活退出的窗口

编程入门 行业动态 更新时间:2024-10-26 14:34:03
本文介绍了使用WindowManager重新激活退出的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在将WPF与Caliburn.Micro(1.4.1)的最新和最新版本一起使用.我使用 IWindowManager.ShowWindow(... )打开一个新的无模式窗口:

I am using WPF with the currently latest and greatest version of Caliburn.Micro (1.4.1). I use IWindowManager.ShowWindow(...) to open an new modeless window:

private void OpenOrReactivateInfoView() { if(this.infoViewModel == null) { this.infoViewModel = new InfoViewModel(); } this.windowManager.ShowWindow(this.infoViewModel); }

我想检查窗口ist是否仍在打开,而不是每次调用OpenOrReactivateInfoView()时都打开一个新窗口,否则现有的窗口应该只是重新获得焦点.

Instead of opening a new window each time when OpenOrReactivateInfoView() is called, I would like to check whether the window ist still open and if it is, the existing window should just regain focus.

我们怎样才能成为一个好的Calibrun.Micro解决方案呢?我当然想避免在viewmodel中保留对窗口(或与此相关的任何UIElement)的引用.还请注意,这是许多无模式对话框的常见行为,因此,最好以通用的可重用方式解决此问题.

What would we be a good Calibrun.Micro-way to solve this? I sure would like to avoid keeping a reference to the window (or any UIElement for that matter) itself in the viewmodel. Also note that this is a common behavior for a lot of modeless dialogs, so it is preferred solve this in a generic reusable way.

Caliburn.Micro是否已经内置了此工具?

Does Caliburn.Micro already have means for this built in?

推荐答案

一种相当简单的方法来跟踪您的窗口,而无需实际 必须实现IViewAware就是保留一个弱引用字典 到一起的ViewModel和View中,然后检查是否已经 是否有现有的Window.可以作为装饰器实现 WindowManager,子类或扩展.

A fairly straightforward way to keep track of your windows without actually having to implement IViewAware would be to keep a dictionary of weak references to your ViewModels and Views that go together and then checking if you already have an existing Window or not. Could be implemented either as a decorator to the WindowManager, subclass or extension.

假设您不这样做,那么以下简单的操作就可以解决问题 实际计划打开足够的窗户,甚至连死掉的WeakReferences 会影响性能.如果要长期运行,不应该 很难实施某种清理.

Something as simple as the following should do the trick assuming you don't actually plan on opening enough windows that even the dead WeakReferences would impact performance. If it is going to be long running it shouldn't be that hard to implement some sort of cleanup.

public class MyFancyWindowManager : WindowManager { IDictionary<WeakReference, WeakReference> windows = new Dictionary<WeakReference, WeakReference>(); public override void ShowWindow(object rootModel, object context = null, IDictionary<string, object> settings = null) { NavigationWindow navWindow = null; if (Application.Current != null && Application.Current.MainWindow != null) { navWindow = Application.Current.MainWindow as NavigationWindow; } if (navWindow != null) { var window = CreatePage(rootModel, context, settings); navWindow.Navigate(window); } else { var window = GetExistingWindow(rootModel); if (window == null) { window = CreateWindow(rootModel, false, context, settings); windows.Add(new WeakReference(rootModel), new WeakReference(window)); window.Show(); } else { window.Focus(); } } } protected virtual Window GetExistingWindow(object model) { if(!windows.Any(d => d.Key.IsAlive && d.Key.Target == model)) return null; var existingWindow = windows.Single(d => d.Key.Target == model).Value; return existingWindow.IsAlive ? existingWindow.Target as Window : null; } }

更多推荐

使用WindowManager重新激活退出的窗口

本文发布于:2023-10-26 09:23:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗口   WindowManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!