1、Windows 8 Metro App开发环境的搭建
2、HelloWorld创建与相关介绍
3、运行和调试
4、应用程序包的创建和安装(本地,不通过商店)
更多内容请查看下面的帖子
[DevDiv原创]Windows 8 Metro App开发Step by Step
1、Windows 8 Metro App开发环境的搭建
在这里安装Windows 8 Release Preview操作系统和Windows Visual Studio 2012 RC就可以进行Metro App开发了。
操作系统安装[我安装的是32位的]
Windows 8 Release Preview(发行预览版,Windows 8系列的最后一个测试版本):
产品密钥:TK8TP-9JN6P-7X7WW-RFFTV-B7QPF
开发工具(只需要安装Windows Visual Studio 2012 RC就可以进行metro app开发)[我安装的是ULtimate旗舰版]
Windows Visual Studio 2012 RC
2、HelloWorld创建与相关介绍
为了更加形象直观,下面我以图文的形式进行介绍
a、从开始画面启动VS2012

b、在起始页选项卡中,选择新建项目来创建HelloWorld,也可以从文件菜单->新建->项目来创建。
如下图,模版选择Visual C# Windows Metro style,右边选择Blank App(XAML),然后在下面输入项目名称,路径和解决方案名称,最后点击确定,就创建好了一个最简单的Metro App。

c、创建好的HelloWorld工程如下图,熟悉VS的开发者可能对此画面不陌生,左边主要是代码编辑,右边三项目文件目录组织。当然,这些界面也是可以配置的。

d、右侧,我们可以看到工程的文件组织,如下图所示,下面我会对其中的重要文件进行介绍。

从上图中,可以看到有如下文件和目录:
1)Properties
2)引用
3)Assets
4)Common
5)App.xaml
6)MainPage.xaml
7)Package.appemanifest
1)Properties目录下的文件Assembly.cs主要存放有关程序集的常规信息和程序的版本信息
01 |
using System.Reflection; |
02 |
using System.Runtime.CompilerServices; |
03 |
using System.Runtime.InteropServices; |
08 |
[assembly: AssemblyTitle("DevDiv_HelloWorld")] |
09 |
[assembly: AssemblyDescription("")] |
10 |
[assembly: AssemblyConfiguration("")] |
11 |
[assembly: AssemblyCompany("")] |
12 |
[assembly: AssemblyProduct("DevDiv_HelloWorld")] |
13 |
[assembly: AssemblyCopyright("Copyright © 2012")] |
14 |
[assembly: AssemblyTrademark("")] |
15 |
[assembly: AssemblyCulture("")] |
24 |
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, |
26 |
// [assembly: AssemblyVersion("1.0.*")] |
27 |
[assembly: AssemblyVersion("1.0.0.0")] |
28 |
[assembly: AssemblyFileVersion("1.0.0.0")] |
29 |
[assembly: ComVisible(false)] |
2)引用,从上面的图中,我们看到默认有两个引用:.NET for Metro style apps和Windows
Metro 应用使用简装版的.NET框架库。我们可以双击Reference查看看到他们对应的命名空间。
3)Assets,该目录下主要存放程序使用到的Logo,启动画面等信息。
4)Common里面有一个文件:
StandardStyles.xaml,它是一个资源文件,它与App.xaml文件进行关联的。其中包含了一些Style和Template,通过这个文件,我们创建应用会变得更容易。
下面我分别摘取了一个Style和Template。
01 |
<Style x:Key="BasicRichTextStyle" TargetType="RichTextBlock"> |
02 |
<Setter Property="Foreground" Value="{StaticResource ApplicationForegroundThemeBrush}"/> |
03 |
<Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/> |
04 |
<Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/> |
05 |
<Setter Property="TextTrimming" Value="WordEllipsis"/> |
06 |
<Setter Property="TextWrapping" Value="Wrap"/> |
07 |
<Setter Property="Typography.StylisticSet20" Value="True"/> |
08 |
<Setter Property="Typography.DiscretionaryLigatures" Value="True"/> |
09 |
<Setter Property="Typography.CaseSensitiveForms" Value="True"/> |
01 |
<DataTemplate x:Key="Standard130ItemTemplate"> |
02 |
<Grid Height="110" Margin="6"> |
03 |
<Grid.ColumnDefinitions> |
04 |
<ColumnDefinition Width="Auto"/> |
05 |
<ColumnDefinition Width="*"/> |
06 |
</Grid.ColumnDefinitions> |
07 |
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="110" Height="110"> |
08 |
<Image Source="{Binding Image}" Stretch="UniformToFill"/> |
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"/> |
5)App.axml
App.xaml 文件和它对应的代码文件是 App.xaml.cs,用于启动 Metro App。这个 XAML 文件的主要用途是与 Common 文件夹中的StandardStyles.xaml进行关联。如下代码所示
02 |
x:Class="DevDiv_HelloWorld.App" |
05 |
xmlns:local="using:DevDiv_HelloWorld"> |
07 |
<Application.Resources> |
09 |
<ResourceDictionary.MergedDictionaries> |
12 |
Styles that define common aspects of the platform look and feel |
13 |
Required by Visual Studio project and item templates |
15 |
<ResourceDictionary Source="Common/StandardStyles.xaml"/> |
16 |
</ResourceDictionary.MergedDictionaries> |
19 |
</Application.Resources> |
下面我们来看看 App.xaml.cs文件中的代码
方便阅读,我们其中的注视去掉了。
02 |
using System.Collections.Generic; |
05 |
using Windows.ApplicationModel; |
06 |
using Windows.ApplicationModel.Activation; |
07 |
using Windows.Foundation; |
08 |
using Windows.Foundation.Collections; |
09 |
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; |
17 |
namespace DevDiv_HelloWorld |
19 |
sealed partial class App : Application |
23 |
this.InitializeComponent(); |
24 |
this.Suspending += OnSuspending; |
27 |
protected override void OnLaunched(LaunchActivatedEventArgs args) |
29 |
if (args.PreviousExecutionState == ApplicationExecutionState.Running) |
31 |
Window.Current.Activate(); |
35 |
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) |
39 |
var rootFrame = new Frame(); |
40 |
if (!rootFrame.Navigate(typeof(MainPage))) |
42 |
throw new Exception("Failed to create initial page"); |
45 |
Window.Current.Content = rootFrame; |
46 |
Window.Current.Activate(); |
49 |
private void OnSuspending(object sender, SuspendingEventArgs e) |
51 |
var deferral = e.SuspendingOperation.GetDeferral(); |
在上面的代码中,有三个方法
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
下面我们就来看看MainPage.xaml。
我们在刚开始创建Blank App时,VS就会为我们创建一个空白的页面,也就是MainPage.xaml。
如下代码:
02 |
x:Class="DevDiv_HelloWorld.MainPage" |
06 |
xmlns:local="using:DevDiv_HelloWorld" |
11 |
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> |
可以看出,其中包含一个Grid控件。而MainPage.xaml.cs的代码则如下[同样,为了代码简洁,我把注视去掉了]:
02 |
using System.Collections.Generic; |
05 |
using Windows.Foundation; |
06 |
using Windows.Foundation.Collections; |
07 |
using Windows.UI.Xaml; |
08 |
using Windows.UI.Xaml.Controls; |
09 |
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; |
15 |
namespace DevDiv_HelloWorld |
17 |
public sealed partial class MainPage : Page |
21 |
this.InitializeComponent(); |
23 |
protected override void OnNavigatedTo(NavigationEventArgs e) |
其中MainPage是初始化函数。OnNavigatedTo方法则是当该页面显示在某个位置的时候,会被调用。
7)Package.appemanifest
最后我们来看看文件Package.appemanifest 。
这是一个提供Metro App信息给Windows runtime的XML文件。信息主要包括:程序显示名称、入口点、支持的旋转、徽标等。