Windows 8实用窍门系列:1.使用Xaml+C#开发第一个Metro Style应用程序

简介:

首先我们需要安装Windows 8以及VS2012,下载地址:http://msdn.microsoft.com/zh-CN/windows/apps/br229516/ 

  然后我们打开VS2012,选择Windows Metro Style,然后选择创建Blank App项目如下图:

  

  其新建完成的项目结构如下: 

     

  我们拖动一个按钮和ListBox到界面中,设置按钮事件以及ListBox的DataTemplate,如下Xaml代码:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Button Content="Button" Name="button1" HorizontalAlignment="Left" 
                Margin="135,124,0,0" VerticalAlignment="Top" Click="button1_Click"/>
        <ListBox HorizontalAlignment="Left" Name="listbox1" Height="100"
                 Margin="135,187,0,0" VerticalAlignment="Top" Width="140">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock  Width="60" Text="{Binding ItemName}"/>                    
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

  其Xaml.cs文件如下:

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            listbox1.ItemsSource = ItemModel.GetItem();
            button1.Content = "Windows 8按钮";
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.button1.Content = this.button1.Content + "1";
        }
    }

  ListBox绑定的数据源代码如下:

    public class ItemModel
    {
        public string ItemName { get; set; }

        public string ItemValue { get; set; }

        public static List<ItemModel> GetItem()
        {
            List<ItemModel> list = new List<ItemModel>();
            list.Add(new ItemModel() { ItemName = "北京", ItemValue = "010" });
            list.Add(new ItemModel() { ItemName = "上海", ItemValue = "020" });
            list.Add(new ItemModel() { ItemName = "成都", ItemValue = "028" });
            return list;
        }
    }

  运行效果图如下: 

     



本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/1009048

相关文章
|
14天前
|
消息中间件 编译器 API
Windows窗口程序
Windows窗口程序
|
15天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
1月前
|
Windows
实现Windows程序的数据更新
实现Windows程序的数据更新
12 0
|
1月前
|
Windows
构建布局良好的Windows程序
构建布局良好的Windows程序
11 0
|
1月前
|
C# Windows
初识Windows程序
初识Windows程序
10 0
|
2月前
|
Java Linux Windows
windows实现自动部署jar包运行程序
windows实现自动部署jar包运行程序
40 0
|
4月前
|
移动开发
【实用】一组原创原生样式的基础控件、UI组件样式(偏向移动端H5页面的样式)
【实用】一组原创原生样式的基础控件、UI组件样式(偏向移动端H5页面的样式)
【实用】一组原创原生样式的基础控件、UI组件样式(偏向移动端H5页面的样式)
|
1月前
|
数据可视化 数据库 C++
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
Qt 5.14.2揭秘高效开发:如何用VS2022快速部署Qt 5.14.2,打造无与伦比的Windows应用
|
2月前
|
Windows
火山中文编程 -- 第一个windows程序
火山中文编程 -- 第一个windows程序
12 0
|
2月前
|
安全 Linux iOS开发
上传 iOS 应用变得更加容易 - 在 Windows 上架 iOS APP 的工具介绍
上传 iOS 应用变得更加容易 - 在 Windows 上架 iOS APP 的工具介绍