Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:36:05+00:00 2026-06-02T01:36:05+00:00

I’ve read a good practice with WPF (and GUI in general) saying to open

  • 0

I’ve read a good practice with WPF (and GUI in general) saying to open as few windows as possible. But sometimes, you simply don’t have the choice.

So I thought about a fast an elegant solution to open a new window and I thought to this:

public static class WinManager
{
    private static Dictionary<Type, Func<Window>> collection 
        = new Dictionary<Type, Func<Window>>();

    /* Bind the type of the ViewModel with a lambda that build an 
     * instance of a window*/
    public static void Bind(Func<Window> ctor, Type type) { ... }

    /* Search in the dictionary the specified type and show the window 
     * returned by the lambda*/
    public static void Show(Type type){ ... }

    /* Search in the dictionary the specified type and show the dialogue
     * returned by the lambda*/
    public static void ShowDialog(Type type) { ... }
}

typeis the type of the ViewModel binded to the View (that’s the window) and the lambda ctor is used to return a fresh instance of a window.

Is it a good idea to manage windows like this or am I totally wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:36:07+00:00Added an answer on June 2, 2026 at 1:36 am

    I think it is a decent idea.

    It has the advantage that the ViewModel that wants to show another window doesn’t have to use any WPF specific code or even know the view. It only needs to know the ViewModel it wants to show the window for. This is very similar to Caliburn.Micros IWindowManager.

    The thing I don’t like about this solution is the static nature of the class. This makes unit testing hard(er). If you are using dependency injection, you could create an interface and an implementation of that interface that is similar to your static class. You could then create an instance of that class in your composition root, bind the ViewModel types to the lambda View factories and register that instance with your DI container. Every ViewModel that wants to show another window now has a dependency on that interface which makes it easily mockable in unit tests.

    Something like this:

    interface IWindowManager
    {
        void Show(Type type);
        void Show<T>();
        void ShowDialog(Type type);
        void ShowDialog<T>();
    }
    
    class WindowManager : IWindowManager
    {
        // Implementation of the four methods from the interface plus:
    
        private Dictionary<Type, Func<Window>> collection 
            = new Dictionary<Type, Func<Window>>();
    
        public void Bind(Func<Window> ctor, Type type) { ... }
    }
    

    Having the Bind method only on the concrete implementation WindowManager has the benefit that consumers of IWindowManager can’t change the registration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.