Windows 8實用竅門系列:1.使用Xaml+C#開發第一個Metro Style應用程式

來源:互聯網
上載者:User

  首先我們需要安裝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;        }    }

  運行如下: 

     

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.