WPF 的 Hello World

LouisJJW 2010-11-01 05:32:57
代码如下:
1)Window1.cs
//Window1.cs

using System;
using System.Windows;
using System.Windows.Controls; // Button et al

namespace 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
//MyApp.cs

using 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
<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.

请问:问题出在哪里?应该如何纠正?
...全文
162 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
GripVips 2010-11-06
  • 打赏
  • 举报
回复
兄弟我真晕你! 不是这样写的啦!
saishangpeiqian 2010-11-05
  • 打赏
  • 举报
回复
楼主太好了[Quote=引用 10 楼 jv9 的回复:]
你从这里下载吧,我吧代码传到这里了:

http://www.silverlightchina.net/resource/code/WpfApplicationDemo.rar
[/Quote]
jv9 2010-11-05
  • 打赏
  • 举报
回复
你从这里下载吧,我吧代码传到这里了:

http://www.silverlightchina.net/resource/code/WpfApplicationDemo.rar
jv9 2010-11-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 louislingjjw 的回复:]
jv9,非常感谢。

因为刚刚开始学习,我用的命令行csc.exe和MSBuild.exe 编译,用记事本编写小程序学习。

我的项目就3个文件,代码全部贴出来了。在最前面:
1)Window1.cs 窗体文件
2)MyApp.cs 主程序
3)1st.csproj 项目文件,引用 Window1.cs
[/Quote]

已经做完了,留下邮箱或者留言邮箱给我,我把源代码发给你。
LouisJJW 2010-11-03
  • 打赏
  • 举报
回复
jv9,非常感谢。

因为刚刚开始学习,我用的命令行csc.exe和MSBuild.exe 编译,用记事本编写小程序学习。

我的项目就3个文件,代码全部贴出来了。在最前面:
1)Window1.cs 窗体文件
2)MyApp.cs 主程序
3)1st.csproj 项目文件,引用 Window1.cs

jv9 2010-11-03
  • 打赏
  • 举报
回复
MSBuild.exe编译?你把项目打包,我帮你调试编译吧。
passself 2010-11-02
  • 打赏
  • 举报
回复
没有发现命名空间,是因为你没有引入或者是没有定义,这样楼主就应该明白了吧
LouisJJW 2010-11-02
  • 打赏
  • 举报
回复
那个错误信息的的意思我知道是“命名空间找不到”

书上说可以运行的,用 MSBuild.exe 编译成 exe.
我想知道如何修改可以让它跑起来.
LouisJJW 2010-11-02
  • 打赏
  • 举报
回复
是《Programming WPF》2nd 第一章 里面 的例子。
jv9 2010-11-01
  • 打赏
  • 举报
回复
void AppStartingUp(object sender, StartingUpCancelEventArgs e) {
// Let the Window1 initialize itself
Window window = new Window1();
window.Show();
}


是你自定义的吧。正常的App中的程序入口应该这样声明,其中引用是using System.Windows;


private void Application_Startup(object sender, StartupEventArgs e)
{
// 代码在这里
}

protected override void OnStartup(StartupEventArgs e)
{

}

protected override void OnExit(ExitEventArgs e)
{

}
冰心的小屋 2010-11-01
  • 打赏
  • 举报
回复
'StartingUpCancelEventArgs'
你在项目里用右键点出错的地方,是否能引用命名空间?
gnimgnot 2010-11-01
  • 打赏
  • 举报
回复
'StartingUpCancelEventArgs'

这个命名空间找不到。

8,757

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧