1.创建一个控制台应用程序
2. 首先要引入三个程序集如下。
//PresentationCore.dll
//PresentationFramework.dll
//System.Xaml
3. 编写window类
using System.Windows;
using System.Windows.Controls;
namespace ConsoleApplication21
{
class MyWindow : Window
{
public Button button;
public MyWindow()
{
Init();
}
private void Init()
{
this.Width = this.Height = 285;
this.Left = this.Top = 100;
this.Title = "Code-only Window";
DockPanel panel = new DockPanel();
this.AddChild(panel);
button = new Button();
panel.Children.Add(button);
button.Content = "Please Click me.";
button.Margin = new Thickness(30);
button.Click += button_Click;
}
public void button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Thanks...");
}
}
}
4. 编写Application类,这里是应用程序的入口点
using System;
using System.Windows;
namespace ConsoleApplication21
{
class Program : Application
{
[STAThread]
static void Main(string[] args)
{
Program p = new Program();
p.MainWindow = new MyWindow();
p.MainWindow.ShowDialog();
}
}
}