初识Windows8应用商店开发

 Windows8从消费者预览版到RTM版的发布让微软成了2012年最受关注的公司,同时这也引发了不少的开发者的兴趣,今天本文将介绍windows 8 开发环境的搭建以及Windows 8 下的第一个程序 HelloWorld。html

 

---------------------------------华丽的分割线-----------------------------------------express

1、Windows 8 开发环境的搭建windows

 Windows 8 的开发须要一台装有Windows 8 的机器,以及visual studio 2012app

  这里我下的是window 8 rtm 企业版 以及visual studio 2012 旗舰版 由于我是学生开发者因此我是在官方网站下的,这里给出我度娘出的结果框架

   Windows 8 专业版32位 http://kuai.xunlei.com/d/BLDJTTBZXIBVide

   Windows 8 专业版64位 http://kuai.xunlei.com/d/VURJIOVUVELH工具

    Visual Studio 2012 专业版 http://www.iplaysoft.com/vs2012.html学习

 

2、HelloWord建立动画

   a.先给出Window 8 的截图网站

 

 

b、在起始页选项卡中,选择新建项目来建立HelloWorld,也能够从文件菜单->新建->项目来建立。

以下图,模版选择Visual C# Windows 应用商店应用,右边选择(XAML),而后在下面输入项目名称,路径和解决方案名称,最后点击肯定,就建立好了一个最简单的Metro App。

c.而后咱们就看到了最熟悉的vs的界面

 

e.右侧咱们看到工程的组织文件

 

 

从上图中,能够看到有以下文件和目录:

1)Properties

2)引用

3)Assets

4)Common

5)App.xaml

6)MainPage.xaml

7)Package.appemanifest


1)Properties目录下的文件Assembly.cs主要存放有关程序集的常规信息和程序的版本信息

 

 

2)引用,从上面的图中,咱们看到默认有两个引用:.NET for Metro style apps和Windows

Metro 应用使用简装版的.NET框架库。咱们能够双击Reference查看看到他们对应的命名空间。

 

3)Assets,该目录下主要存放程序使用到的Logo,启动画面等信息。

 

4)Common里面有一个文件:

StandardStyles.xaml,它是一个资源文件,它与App.xaml文件进行关联的。其中包含了一些Style和Template,经过这个文件,咱们建立应用会变得更容易。
下面我分别摘取了一个Style和Template。

 

 

 这是定义的style

  
  
  
  
  1. <Style x:Key="BasicRichTextStyle" TargetType="RichTextBlock"> 
  2.     <Setter Property="Foreground" Value="{StaticResource ApplicationForegroundThemeBrush}"/> 
  3.     <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/> 
  4.     <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/> 
  5.     <Setter Property="TextTrimming" Value="WordEllipsis"/> 
  6.     <Setter Property="TextWrapping" Value="Wrap"/> 
  7.     <Setter Property="Typography.StylisticSet20" Value="True"/> 
  8.     <Setter Property="Typography.DiscretionaryLigatures" Value="True"/> 
  9.     <Setter Property="Typography.CaseSensitiveForms" Value="True"/> 
  10. </Style> 

 这是定义的数据显示的模版

  
  
  
  
  1. <DataTemplate x:Key="Standard130ItemTemplate"> 
  2.     <Grid Height="110" Margin="6"> 
  3.         <Grid.ColumnDefinitions> 
  4.             <ColumnDefinition Width="Auto"/> 
  5.             <ColumnDefinition Width="*"/> 
  6.         </Grid.ColumnDefinitions> 
  7.         <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110"> 
  8.             <Image Source="{Binding Image}" Stretch="UniformToFill"/> 
  9.         </Border> 
  10.         <StackPanel Grid.Column="1" VerticalAlignment="Top" Margin="10,0,0,0"> 
  11.             <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextStyle}" TextWrapping="NoWrap"/> 
  12.             <TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap"/> 
  13.             <TextBlock Text="{Binding Description}" Style="{StaticResource BodyTextStyle}" MaxHeight="60"/> 
  14.         </StackPanel> 
  15.     </Grid> 
  16. </DataTemplate> 

5)App.axml
App.xaml 文件和它对应的代码文件是 App.xaml.cs,用于启动 Metro App。这个 XAML 文件的主要用途是与 Common 文件夹中的StandardStyles.xaml进行关联。以下代码所示

 

  
  
  
  
  1. <Application 
  2.     x:Class="App.App" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:local="using:App"> 
  6.  
  7.     <Application.Resources> 
  8.         <ResourceDictionary> 
  9.             <ResourceDictionary.MergedDictionaries> 
  10.  
  11.                 <!--  
  12.                     用于定义平台外观的共同方面的样式 
  13.                     Visual Studio 项目和项模板所必需的 
  14.                  --> 
  15.                 
  16.     
        
        
        
    1. <ResourceDictionary Source="Common/StandardStyles.xaml"/> 
  17.  
  18.             </ResourceDictionary.MergedDictionaries> 
  19.  
  20.         </ResourceDictionary> 
  21.     </Application.Resources> 
  22. </Application> 

注意这里

<ResourceDictionary Source="Common/StandardStyles.xaml"/>

这一句将Common文件夹下的StandardStyles.xaml注册到App.xaml 中一边其余成员可使用定义的样式 以及数据显示的模版

对应App.xaml 有一个App.xaml.cs

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.IO; 
  4. using System.Linq; 
  5. using Windows.ApplicationModel; 
  6. using Windows.ApplicationModel.Activation; 
  7. using Windows.Foundation; 
  8. using Windows.Foundation.Collections; 
  9. using Windows.UI.Xaml; 
  10. using Windows.UI.Xaml.Controls; 
  11. using Windows.UI.Xaml.Controls.Primitives; 
  12. using Windows.UI.Xaml.Data; 
  13. using Windows.UI.Xaml.Input; 
  14. using Windows.UI.Xaml.Media; 
  15. using Windows.UI.Xaml.Navigation; 
  16.  
  17. // “空白应用程序”模板在 http://go.microsoft.com/fwlink/?LinkId=234227 上有介绍 
  18.  
  19. namespace App 
  20.     /// <summary> 
  21.     /// 提供特定于应用程序的行为,以补充默认的应用程序类。 
  22.     /// </summary> 
  23.     sealed partial class App : Application 
  24.     { 
  25.         /// <summary> 
  26.         /// 初始化单一实例应用程序对象。这是执行的创做代码的第一行, 
  27.         /// 逻辑上等同于 main() 或 WinMain()。 
  28.         /// </summary> 
  29.         public App() 
  30.         { 
  31.             this.InitializeComponent(); 
  32.             this.Suspending += OnSuspending; 
  33.         } 
  34.  
  35.         /// <summary> 
  36.         /// 在应用程序由最终用户正常启动时进行调用。 
  37.         /// 当启动应用程序以执行打开特定的文件或显示搜索结果等操做时 
  38.         /// 将使用其余入口点。 
  39.         /// </summary> 
  40.         /// <param name="args">有关启动请求和过程的详细信息。</param> 
  41.         protected override void OnLaunched(LaunchActivatedEventArgs args) 
  42.         { 
  43.             Frame rootFrame = Window.Current.Content as Frame; 
  44.  
  45.             // 不要在窗口已包含内容时重复应用程序初始化, 
  46.             // 只需确保窗口处于活动状态 
  47.             if (rootFrame == null) 
  48.             { 
  49.                 // 建立要充当导航上下文的框架,并导航到第一页 
  50.                 rootFrame = new Frame(); 
  51.  
  52.                 if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) 
  53.                 { 
  54.                     //TODO: 从以前挂起的应用程序加载状态 
  55.                 } 
  56.  
  57.                 // 将框架放在当前窗口中 
  58.                 Window.Current.Content = rootFrame
  59.             } 
  60.  
  61.             if (rootFrame.Content == null) 
  62.             { 
  63.                 // 当未还原导航堆栈时,导航到第一页, 
  64.                 // 并经过将所需信息做为导航参数传入来配置 
  65.                 // 参数 
  66.                 if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)) 
  67.                 { 
  68.                     throw new Exception("Failed to create initial page"); 
  69.                 } 
  70.             } 
  71.             // 确保当前窗口处于活动状态 
  72.             Window.Current.Activate(); 
  73.         } 
  74.  
  75.         /// <summary> 
  76.         /// 在将要挂起应用程序执行时调用。在不知道应用程序 
  77.         /// 将被终止仍是恢复的状况下保存应用程序状态, 
  78.         /// 并让内存内容保持不变。 
  79.         /// </summary> 
  80.         /// <param name="sender">挂起的请求的源。</param> 
  81.         /// <param name="e">有关挂起的请求的详细信息。</param> 
  82.         private void OnSuspending(object sender, SuspendingEventArgs e) 
  83.         { 
  84.             var deferral = e.SuspendingOperation.GetDeferral(); 
  85.             //TODO: 保存应用程序状态并中止任何后台活动 
  86.             deferral.Complete(); 
  87.         } 
  88.     } 

 在上面的代码中,有三个方法

public App()
    该方法用于初始化一个应用程序,咱们所写的代码,这里是首先被执行的,逻辑上等价于main() 或WinMain()。

protected override void OnLaunched(LaunchActivatedEventArgs args)
    当用户正常状况下启动完毕了程序,该方法会被调用。固然,也有其它状况下,该方法会被调用,例如:当启动程序以打开指定的文件,或者显示搜索结果,等等。

private void OnSuspending(object sender, SuspendingEventArgs e)
    当程序被停止时,该方法会被调用。

应用程序的生命周期主要就是经过App.xaml.cs文件进行处理的,关于Metro App的生命周期,在后面的学习中,我会进行介绍。如今你只须要知道OnLanunched方法是在程序启动的时候被调用的。在该方法中,会建立并加载MainPage的一个实例,做为应用程序的主入口。

6.MainPage.xaml 是用来定义咱们运行时,看到的界面,这是默认生成的,固然咱们能够本身定义开始的导航界面。

 

  
  
  
  
  1. <Page 
  2.     x:Class="App.MainPage" 
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  5.     xmlns:local="using:App" 
  6.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  7.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  8.     mc:Ignorable="d"> 
  9.  
  10.     <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
  11.  
  12.     </Grid> 
  13. </Page> 

这是我第一次接触到XAML,可是我的以为只要有地XML或者HTML经验的人看XAML的时候应该是没什么大问题

在这里MainPage.xaml就是在包含了一个Grid的控件

固然这里的MainPage.xaml也包含一个隐藏的文件MainPage.xaml.cs

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.IO; 
  4. using System.Linq; 
  5. using Windows.Foundation; 
  6. using Windows.Foundation.Collections; 
  7. using Windows.UI.Xaml; 
  8. using Windows.UI.Xaml.Controls; 
  9. using Windows.UI.Xaml.Controls.Primitives; 
  10. using Windows.UI.Xaml.Data; 
  11. using Windows.UI.Xaml.Input; 
  12. using Windows.UI.Xaml.Media; 
  13. using Windows.UI.Xaml.Navigation; 
  14.  
  15. namespace App 
  16.     public sealed partial class MainPage : Page 
  17.     { 
  18.         public MainPage() 
  19.         { 
  20.             this.InitializeComponent(); 
  21.         } 
  22.  
  23.         protected override void OnNavigatedTo(NavigationEventArgs e) 
  24.         { 
  25.         } 
  26.     } 

这里MainPage()是用来初始化用的,OnNavigatedTo方法则是当该页面显示在某个位置的时候,会被调用。


7)Package.appemanifest

最后咱们来看看文件Package.appemanifest 。

这是一个提供Metro App信息给Windows runtime的XML文件。信息主要包括:程序显示名称、入口点、支持的旋转、徽标等。

 

3、运行 和 调试

 

一、如图我利用了visual studio 2012提供的界面设计工具 给空白页上添加了一个TextBlock控件并添加上文字,这样就算给这个HelloWorld程序设计好了界面(PS:虽然很丑)

 

点击下面的按钮 

 

 这样咱们就成功地完成了世界上“最伟大”的程序