27. February 2009 10:30
Sometimes, you need some functionality that is not included in the .NET framework. Extension methods are a (some people think bad) way to add functionality without the need to subclass a class (such as Window) and derive all the new classes that need this functionality from your customized class.
For one of my clients, I have developed a add-in based system that is based on the Add-In namespace. The Add-In namespace is a great way to split functionality into separate add-ins that you can load on-demand. However, there are some disadvantages. For example, Windows that you show as a dialog window in an add-in don’t get the right owner. However, you can’t touch the real owner (the add-in host) since you cannot serialize a Window object.
To overcome such problems, I have written some extension methods. Some of them are described below. The source code for these extension methods can be downloaded at the bottom of this post.
WindowExtensions.SetOwnerWindow (3 overloads)
This method sets the owner of a Window. If the Owner property of a the Window object, the Owner will be kept because it is explicitly set by the developer. However, in case that the Owner window is not set, the method tries to find the main window of the process. This way, the Window will be displayed modal to the main window of the process.
Sometimes, you want to bring a Window to the top of the desktop. Activate will not always work in case you want to force the window to show on top of all windows. But, you don’t want to make the window system modal either, because the user must be able to focus other windows as well. In that case, especially when you are using the Add-In namespace when you can’t get to the main window directly, this method is very helpful.
Update (2009-03-03): I have made some small modifications so that existing owners will be respected.
CatenaLogic.Windows.Presentation.zip (4.12 kb) [Downloads: 652]