WPF 的 Hello World
代码如下:
1)Window1.cs
//Window1.csusing System;using System.Windows;using System.Windows.Controls; // Button et alnamespace MyFirstWpfApp { class Window1 : Window { public Window1() { this.Text = "Hello, Wpf"; // Do something interesting Button button = new Button(); button.Content = "Click me, baby, one more time!"; button.Width = 200; button.Height = 25; button.Click += button_Click; this.AddChild(button); } void button_Click(object sender, RoutedEventArgs e) { MessageBox.Show("You've done that before, haven't you...", "Nice!"); } }}
//MyApp.csusing System;using System.Windows;namespace MyFirstWpfApp { class MyApp : Application { [STAThread] static void Main(string[] args) { MyApp app = new MyApp(); app.StartingUp += app.AppStartingUp; app.Run(args); } void AppStartingUp(object sender, StartingUpCancelEventArgs e) { // Let the Window1 initialize itself Window window = new Window1(); window.Show(); } }}
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <OutputType>winexe</OutputType> <OutputPath>.\</OutputPath> <Assembly>1st.exe</Assembly> </PropertyGroup> <ItemGroup> <Compile Include="MyApp.cs" /> <Compile Include="Window1.cs" /> <Reference Include="System" /> <Reference Include="WindowsBase" /> <Reference Include="PresentationCore" /> <Reference Include="PresentationFramework" /> </ItemGroup> <Import Project="$(MsbuildBinPath)\Microsoft.CSharp.targets" /></Project>