Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
Dear all,

I have problem with WPF Windows in my application. I created empty c# project and within it Program class with only Main() method. Here is the whole code:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace myNamespace
{
    class Program
    {
        [STAThread]
        static void Main()
        {
            Window w = new Window();
            w.Show();
        }
    }
}


When compiled and run this code produces an empty window, which closes immediately. How can I make my window to stay opened and wait for user to close it?

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Feb-11 15:42pm    
This question if very important. Usually, you don't have to do this: auto-generated code behind does it all for you. But this does not allow, for example, to convert WPF application into library to be used by other application(s). The way to do it using the code you're trying to write. Instead of "main" you could have a public method use by another assembly. You can also write regular application in this style; it may make sense. By the way, what's your purpose?

You should better re-formulate this excellent question (something like "how to create WPF application without app.xaml) and explain your motivation.

I also had this problem and found the solution, will share it a little later.
--SA
Sergey Alexandrovich Kryukov 5-Feb-11 17:14pm    
Done.
--SA
dsnlkc 6-Feb-11 3:28am    
My purpose of not using "WPF Application" project template to create my window is that I do not know what it should look like at compile time, so I need to parse some XAML at run time to create it. I don't think that "WPF Application" project template offers such functionality, but I might be wrong and if I am, please correct me.

1 solution

This is an excellent and important question! Please see my preliminary comment to the question.

Here is my answer. You're actually already close to the solution. Let's add just a bit.
Let's assume you created WPF application, then removed everything redundant, including app.xaml. That will make a project to fail compilation, because entry point is not found. You already added entry point correctly. Let's assume you either renamed the window created by existing Window1.xaml code-behind to WindowMain. What you actually did was creating and instance of original class System.Windows.Window, but I want assume to subclass it:

C#
public partial class WindowMain : System.Windows.Window
{ /*...*/ }


I also want to sub-class application; again, it is not relevant to your question but very useful to make your solution realistic; you may want to add useful properties to your derived application class. Also, it's most convenient to put the entry point in this class:

C#
class CodedApplication : System.Windows.Application {

    [STAThread]
    static void Main(string[] commandLine) {
        CodedApplication application = new CodedApplication();
        application.MainWindow = new WindowMain();
        application.MainWindow.Show();
        application.Run();
    } //Main

} //class CodedApplication


Now, it will work! Note that I only added two lines to your function, but those lines are essential, they initialize and run the Application. The Window along cannot work outside the context of application.

Now see how useful this approach is!
You can add many important properties to your application class, some universal or some specific to your application field — I would advice you make separate layer with yet another sub-classing step. You can add more Windows to the project; some of them can be purely manually coded, for others (including your main Windows) you can still use XAML and code-behind.

Most importantly, you can make it a library and use in more than one application!

Thanks for a good question again and good luck,
—SA
 
Share this answer
 
v8
Comments
Sergey Alexandrovich Kryukov 5-Feb-11 17:11pm    
Edited code: removed qualified name from the WindowMain because it was the name space I used for testing not shown in the sample.
--SA
dsnlkc 6-Feb-11 3:38am    
Great answer. Thanks.

Application class seems to have some really nice functionalities. I have two questions, though.

How to add newly created window to its Windows property?
How to hide the console which shows before the first window?

Thanks again.
Sergey Alexandrovich Kryukov 6-Feb-11 12:26pm    
Thank you for accepting my answer. There are more to it, but we will be able to discuss it later, if you want. Your two questions:

1) You can work with all other windows through main window (or any other windows); you just create a window, make other window its owner, and show. (I would recommend not having more than one, using tab interface, docking interface or whatever; and also no or minimum of modal windows (dialog-like), but that's up to you.)

2) This console will go. It's only shown because you set this as your project option, initially. I think you simply used VS project template "Console Application" to start with. When you go to project's "Properties", and, if the first "Application" tab, change "Output type" to "Windows Application" or "Class Library", it will go. Your entry assembly (.exe) should be "Windows Application" which actually differs from "Console Application" in one option: "show console".

--SA
dsnlkc 6-Feb-11 13:12pm    
First answer is pretty much what I expected. Thanks for the tip on user interface design; minimalistic (in the sense of quantity of windows) user interface is more to my liking, too. Second answer was an enlightenment. You saved me A LOT of time. I think I have all I need in order to write my application now, but if something related come up I will post the question here as a reply.

Thanks again and happy coding!
Sergey Alexandrovich Kryukov 6-Feb-11 16:15pm    
Very happy to hear that, thank you.
Basically, you should know 2 things. You should 1) be able to precisely tell your source code from any non-source code, generated, intermediate, etc. to be able to get rid of non-source (all that obj/*, *.suo, *.user) and not care about it; 2) know every detail of project structure (and delete what you don't use, all that auto-generated but unused references, resources, configuration, and, in your case, even app.xaml and its code-behind).
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900