Windows 8 Hello World
Windows 8 的metro风格应用程序的开发编程和Windows phone 7是非常类似的,不过Windows 8对开发语言的支持是比Windows Phone 7强大很多,支持C++,C#和JavaScript,而Windows Phone 7只是支持C#,当然Windows Phone 8出来之后就会支持C#和C++的开发了。下面是Windows 8的编程体系图。其实Windows 8就是在Windows 7的基础上加上了metro的程序框架,通常所说的Windows 8的开发技术就是指Windows8的metro程序开发。
1、Windows 8 整个系统分成了Metro style和Desktop两个体系,而WinRT则是全新的Metro应用程序架构的基础所在;
2、WinRT具备了多语言的支持能力,支持C++,C#,VB,JavaScript;
3、WinRT和Win32、.NET是相互独立的API体系;
4、WinRT是专门为触屏体验的全新的API。
下面来看一下一个C++的Windows 8的hello world程序
//
// App.xaml.h
// App 类的声明。
//
#pragma once
#include "App.g.h"
namespace HelloWorld_C__
{
///
/// 提供特定于应用程序的行为,以补充默认的应用程序类。
///
ref class App sealed
{
public:
App();
virtual void OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ pArgs) override;
private:
void OnSuspending(Platform::Object^ sender, Windows::ApplicationModel::SuspendingEventArgs^ e);
};
}
//
// App.xaml.cpp
// App 类的实现。
//
#include "pch.h"
#include "MainPage.xaml.h"
using namespace HelloWorld_C__;
using namespace Platform;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Activation;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Interop;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
// “空白应用程序”模板在 http://go.microsoft.com/fwlink/?LinkId=234227 上提供
///
/// 初始化单一实例应用程序对象。这是执行的创作代码的第一行,
/// 逻辑上等同于 main() 或 WinMain()。
///
App::App()
{
InitializeComponent();
Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
}
///
/// 在应用程序由最终用户正常启动时进行调用。
/// 当启动应用程序以执行打开特定的文件或显示搜索结果等操作时
/// 将使用其他入口点。
///
/// Details about the launch request and process.
void App::OnLaunched(Windows::ApplicationModel::Activation::LaunchActivatedEventArgs^ pArgs)
{
// Do not repeat app initialization when already running, just ensure that
// the window is active
if (pArgs->PreviousExecutionState == ApplicationExecutionState::Running)
{
Window::Current->Activate();
return;
}
if (pArgs->PreviousExecutionState == ApplicationExecutionState::Terminated)
{
//TODO: 从之前挂起的应用程序加载状态
}
// 创建一个 Frame 以用作导航上下文并导航至第一页
auto rootFrame = ref new Frame();
if (!rootFrame->Navigate(TypeName(MainPage::typeid)))
{
throw ref new FailureException("Failed to create initial page");
}
// 将 Frame 放置在当前窗口中并确保其处于活动状态
Window::Current->Content = rootFrame;
Window::Current->Activate();
}
///
/// 在将要挂起应用程序执行时调用。在不知道应用程序
/// 将被终止还是恢复的情况下保存应用程序状态,
/// 并让内存内容保持不变。
///
/// 挂起的请求的源。
/// 有关挂起的请求的详细信息。
void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
{
(void) sender; // Unused parameter
(void) e; // Unused parameter
//TODO: 保存应用程序状态并停止任何后台活动
}
App.xaml文件
MainPage.xaml
//MainPage.xaml.h
#pragma once
#include "MainPage.g.h"
namespace HelloWorld_C__
{
public ref class MainPage sealed
{
public:
MainPage();
protected:
virtual void OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs^ e) override;
};
}
//MainPage.xaml.cpp
#include "pch.h"
#include "MainPage.xaml.h"
using namespace HelloWorld_C__;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
MainPage::MainPage()
{
InitializeComponent();
this->myTextBlock->Text="Hello World";
}
void MainPage::OnNavigatedTo(NavigationEventArgs^ e)
{
(void) e; // Unused parameter
}
再来看看项目的结构
App.xaml:应用程序对象和Wp7里面的一样,App.xaml.h, App.xaml.cpp:Application相关事件和处理。
MainPage.xaml.h, MainPage.xaml.cpp:包含默认页面UI的event和基本逻辑,但不包含MainPage.xaml里UI生成的代码。
Package.appxmanifest:定义App相关的基本信息。包括App名字,描述,logo等。
pch.h, pch.cpp: 预编译文件。
Assets文件里面存放程序的logo等相关的图片,以前Wp7是直接放到外面的根目录下的。
运行的效果
页:
[1]