Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

New Post: reactivate exiting window using WindowManager

$
0
0

I have come up with this extension method. It works but I am not particulary happy with it, it is still somewhat hackish. Do you see any other issues with it? 

using System;
using System.Collections.Generic;
using Caliburn.Micro;

publicstaticclass WindowManagerExtensions
{
    ///<summary>/// Shows a non-modal window for the specified model or refocuses the exsiting window.  ///</summary>///<remarks>/// If the model is already associated with a view and the view is a window that window will just be refocused/// and the parameter <paramref name="settings"/> is ignored.///</remarks>publicstaticvoid FocusOrShowWindow(this IWindowManager windowManager,
                                         object model,
                                         object context = null,
                                         IDictionary<string, object> settings = null)
    {
        var activate = model as IActivate;
        if (activate == null)
        {
            thrownew ArgumentException(
                string.Format("An instance of type {0} is required", typeof (IActivate)), "model");
        }

        var viewAware = model as IViewAware;
        if (viewAware == null)
        {
            thrownew ArgumentException(
                string.Format("An instance of type {0} is required", typeof (IViewAware)), "model");
        }

        if (!activate.IsActive)
        {
            windowManager.ShowWindow(model, context, settings);
            return;
        }

        var view = viewAware.GetView(context);
        if (view == null)
        {
            thrownew InvalidOperationException("View aware that is active must have an attached view.");
        }

        var focus = view.GetType().GetMethod("Focus");
        if (focus == null)
        {
            thrownew InvalidOperationException("Attached view requires to have a Focus method");
        }

        focus.Invoke(view, null);
    }
}

Viewing all articles
Browse latest Browse all 1760

Trending Articles