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

相关文章
|
1月前
|
物联网 数据处理 C#
C#实现上位机开发,串口通信,读写串口数据并处理16进制数据
C#实现上位机开发,串口通信,读写串口数据并处理16进制数据。在自动化、物联网以及工业控制行业中,上位机开发是一项重要的技能。本教程主要介绍使用C#进行上位机开发,重点在于串口通信和数据处理。
333 82
|
1月前
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
37 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
20天前
|
Windows
Windows程序的数字签名证书怎么申请
Windows程序的数字签名证书申请流程包括:准备企业资料(营业执照、税务登记证等),提交申请表及企业资料。经过初审、实名认证和二审后,等待1-5个工作日审核结果。审核通过后,CA机构颁发证书并通过邮件或邮寄方式发送。收到证书后按指南安装并使用签名工具对程序进行数字签名,确保软件完整性和可信度。注意证书有效期、管理和兼容性问题。
|
23天前
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
41 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
|
3月前
|
缓存 算法 安全
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
精选10款C#/.NET开发必备类库(含使用教程),工作效率提升利器!
121 12
|
3月前
|
Linux C# iOS开发
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
开源GTKSystem.Windows.Forms框架让C# Winform支持跨平台运行
82 12
|
4月前
|
缓存 C# 开发者
C# 一分钟浅谈:Blazor Server 端开发
本文介绍了 Blazor Server,一种基于 .NET 的 Web 开发模型,允许使用 C# 和 Razor 语法构建交互式 Web 应用。文章从基础概念、创建应用、常见问题及解决方案、易错点及避免方法等方面详细讲解,帮助开发者快速上手并提高开发效率。
121 2
|
4月前
|
开发框架 缓存 .NET
C# 一分钟浅谈:Blazor Server 端开发
Blazor Server 是基于 ASP.NET Core 的框架,允许使用 C# 和 Razor 语法构建交互式 Web 应用。本文介绍 Blazor Server 的基本概念、快速入门、常见问题及解决方案,帮助开发者快速上手。涵盖创建应用、基本组件、数据绑定、状态管理、跨组件通信、错误处理和性能优化等内容。
240 1
|
4月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(上)
|
4月前
|
数据库连接 数据库 C#
Windows下C# 通过ADO.NET方式连接南大通用GBase 8s数据库(下)
本文接续前文,深入讲解了在Windows环境下使用C#和ADO.NET操作南大通用GBase 8s数据库的方法。通过Visual Studio 2022创建项目,添加GBase 8s的DLL引用,并提供了详细的C#代码示例,涵盖数据库连接、表的创建与修改、数据的增删查改等操作,旨在帮助开发者提高数据库管理效率。