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:
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:
class CodedApplication : System.Windows.Application {
[STAThread]
static void Main(string[] commandLine) {
CodedApplication application = new CodedApplication();
application.MainWindow = new WindowMain();
application.MainWindow.Show();
application.Run();
}
}
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