• 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的开发技术就是指Windows  8的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__
    {
        /// <summary>
        /// 提供特定于应用程序的行为,以补充默认的应用程序类。
        /// </summary>
        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 上提供
    
    /// <summary>
    /// 初始化单一实例应用程序对象。这是执行的创作代码的第一行,
    /// 逻辑上等同于 main() 或 WinMain()。
    /// </summary>
    App::App()
    {
        InitializeComponent();
        Suspending += ref new SuspendingEventHandler(this, &App::OnSuspending);
    }
    
    /// <summary>
    /// 在应用程序由最终用户正常启动时进行调用。
    /// 当启动应用程序以执行打开特定的文件或显示搜索结果等操作时
    /// 将使用其他入口点。
    /// </summary>
    /// <param name="pArgs">Details about the launch request and process.</param>
    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();
    }
    
    /// <summary>
    /// 在将要挂起应用程序执行时调用。在不知道应用程序
    /// 将被终止还是恢复的情况下保存应用程序状态,
    /// 并让内存内容保持不变。
    /// </summary>
    /// <param name="sender">挂起的请求的源。</param>
    /// <param name="e">有关挂起的请求的详细信息。</param>
    void App::OnSuspending(Object^ sender, SuspendingEventArgs^ e)
    {
        (void) sender;    // Unused parameter
        (void) e;    // Unused parameter
    
        //TODO: 保存应用程序状态并停止任何后台活动
    }
    App.xaml文件
    <
    Application x:Class="HelloWorld_C__.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:HelloWorld_C__"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!-- Styles that define common aspects of the platform look and feel Required by Visual Studio project and item templates --> <ResourceDictionary Source="Common/StandardStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application>

     MainPage.xaml

    <Page 
        x:Class="HelloWorld_C__.MainPage"
        IsTabStop="false"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:HelloWorld_C__"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <TextBlock  x:Name="myTextBlock"  TextAlignment="Center" FontSize="60" />
        </Grid>
    </Page>
    //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是直接放到外面的根目录下的。

     运行的效果

  • 相关阅读:
    antd vue如何在父组件里打开子组件(子组件是个模态框)?
    webpack知识点整理
    JavaScript中常用的方法汇总,全部整理好了,一定要收藏!
    从地址栏里面获取参数的值
    何为垫片?垫片是一种什么概念在js中?
    async 与 await使用
    纯手撸Promise
    如何使用 Promise?
    Notification 浏览器桌面通知的使用
    学习骨架屏(Skeleton Screens)技术
  • 原文地址:https://www.cnblogs.com/linzheng/p/2571281.html
Copyright © 2020-2023  润新知