如何在空的C#项目中使用WPF窗口? [英] How to use WPF windows in empty c# project?

查看:99
本文介绍了如何在空的C#项目中使用WPF窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我在应用程序中遇到了WPF Windows的问题.我创建了一个空的c#项目,并在其中创建了仅Main()方法的Program类.这是完整的代码:

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:

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.

推荐答案

这是一个非常重要的重要问题!请参阅我对该问题的初步评论.

这是我的答案.您实际上已经接近解决方案.让我们加一点.
假设您创建了WPF应用程序,然后删除了所有多余的内容,包括app.xaml.这将使项目无法编译,因为找不到入口点.您已经正确添加了入口点.假设您将现有Window1.xaml代码后台创建的窗口重命名为WindowMain.您实际上所做的是创建和创建原始类System.Windows.Window的实例,但是我想假设将其子类化:

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();
    } //Main

} //class CodedApplication



现在,它将起作用!请注意,我只向您的函数添加了两行,但是这些行是必不可少的,它们会初始化并运行应用程序. Window无法在应用程序上下文之外工作.

现在看看这种方法有多么有用!
您可以为应用程序类添加许多重要属性,可以为应用程序字段添加一些通用属性或某些特定属性-我建议您使用另一个子类化步骤来创建单独的层.您可以在项目中添加更多Windows.其中一些可以完全由人工编码,而对于其他一些(包括您的主Windows),您仍然可以使用XAML和后台代码.

最重要的是,您可以使其成为一个库并在多个应用程序中使用!

再次感谢您提出了一个很好的问题,祝您好运,

—SA



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


这篇关于如何在空的C#项目中使用WPF窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆