WPF 的 Hello World
代码如下:
1)Window1.cs
- C# code
//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!"); } }}
2)MyApp.cs
- C# code
//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(); } }}
3)1st.csproj
- C# code
<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>
编译:MSBuild 1st.csproj
报错:
MyApp.cs(14,39): error CS0246: The type or namespace name 'StartingUpCancelEventArgs' could not be found (are you missing a using directive or an assembly reference?) [C:\tmp\1st.csproj]
Done Building Project "C:\tmp\1st.csproj" (default targets) -- FAILED.
请问:问题出在哪里?应该如何纠正?
[解决办法]
'StartingUpCancelEventArgs'
这个命名空间找不到。
[解决办法]
'StartingUpCancelEventArgs'
你在项目里用右键点出错的地方,是否能引用命名空间?
[解决办法]
没有发现命名空间,是因为你没有引入或者是没有定义,这样楼主就应该明白了吧
[解决办法]
你从这里下载吧,我吧代码传到这里了:
http://www.silverlightchina.net/resource/code/WpfApplicationDemo.rar
[解决办法]
楼主太好了
[解决办法]
兄弟我真晕你! 不是这样写的啦!