使用WindowManager重新激活退出的窗口 [英] reactivate exiting window using WindowManager

查看:156
本文介绍了使用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重新激活退出的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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