且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

体验Win8 Metro应用开发

更新时间:2022-06-08 21:38:37

装了Windows8 Consumer Preview和Visual Studio 2011 Beta,体验一把Metro风格应用开发,先写个HelloWorld试试。

Metro Style App支持多种开发方式,包括:

    XAML/C# 或 XAML/VB.Net
    HTML/JS
    XAML/C++
    DirectX/C++

也可以把几种技术混合起来进行开发,我用XAML/C#先试一下。


1、建工程

在Win8环境下,建工程时能看到有Windows Metro style,这个选项在非Win8的环境下是看不到的。建个Blank Application吧,这个最简单。

体验Win8 Metro应用开发


2、写界面XAML(BlankPage.xaml)

只放两个TextBlock,显示文本信息用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<Page 
    x:Class="HelloWorld.BlankPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:HelloWorld" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"
  
    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}"
        <StackPanel
            <!--在界面上放两个TextBlock,一个显示Hello World,一个显示时间--> 
            <TextBlock FontSize="60" HorizontalAlignment="Center" Margin="0,200,0,0">Hello World!</TextBlock
            <TextBlock Name="txtCurrentTime" FontSize="48" HorizontalAlignment="Center" Margin="0,50,0,0"></TextBlock
        </StackPanel
    </Grid
</Page>


3、写代码(BlankPage.xaml.cs)

在OnNavigatedTo方法中写几行代码,让界面上第二个TextBlock显示当前时间。

1
2
3
4
5
6
7
8
9
10
protected override void OnNavigatedTo(NavigationEventArgs e) 
    //建个定时器,每秒更新一下txtCurrentTime的Text为当前时间 
    var timer = new DispatcherTimer(); 
    timer.Interval = TimeSpan.FromSeconds(1); 
    timer.Tick += (sender, args) => { 
        txtCurrentTime.Text = DateTime.Now.ToString(); 
    }; 
    timer.Start(); 
}



OK了,Ctrl+F5跑一下,效果如下:

体验Win8 Metro应用开发

PS:在Win8上开发Metro应用,需要先注册一个开发者许可。





     本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/859804,如需转载请自行联系原作者