第 2 页
For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.allitebooks.com
For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.allitebooks.com
Contents at a Glance About the Author ������������������������������������������������������������������������������ xi About the Technical Reviewer �������������������������������������������������������� xiii Acknowledgments��������������������������������������������������������������������������� xv ■Chapter 1: Getting Started�������������������������������������������������������������� 1 ■Chapter 2: Data, Binding, and Pages �������������������������������������������� 21 ■Chapter 3: AppBars, Flyouts, and Navigation������������������������������� 45 ■Chapter 4: Views and Tiles����������������������������������������������������������� 69 ■Chapter 5: App Life Cycle and Contracts�������������������������������������� 95 Index���������������������������������������������������������������������������������������������� 115 v www.allitebooks.com
Chapter 1 Getting Started Windows Store apps are an important addition to Microsoft Windows 8, providing the cornerstone for a single, consistent programming and interaction model across desktops, tablets, and smartphones. The app user experience is very different from previous generations of Windows applications: Windows Store apps are full-screen and favor a style that is simple, direct, and free from distractions. Windows Store apps represent a complete departure from previous versions of Windows. There are entirely new APIs, new interaction controls, and a very different approach to managing the life cycle of applications. Windows Store apps can be developed using a range of languages, including JavaScript, Visual Basic, C++, and, the topic of this book, C#. Windows 8 builds on the familiar to let developers use their existing C# and XAML experience to build rich apps and integrate into the wider Windows platform. This book gives you an essential jump start into the world of Windows Store apps; by the end, you will understand how to use the controls and features that define the core Windows Store app experience. ■ Note Microsoft uses the term Windows Store App, which I find awkward, and I can’t bring myself to use it throughout this book. Instead, I’ll refer to Windows apps and, often, just plain apps. I’ll leave you to mentally insert the official Microsoft names as you see fit. About This Book This book is for experienced C# developers who want to get a head start creating apps for Windows 8. I explain the concepts and techniques you need to get up to speed quickly and to boost your app development techniques and knowledge What Do You Need to Know Before You Read This Book? You need to have a good understanding of C# and, ideally, of XAML. If you have worked on WPF or Silverlight projects, then you will have enough XAML knowledge to build Windows apps. Don’t worry if you haven’t worked with XAML before; you can pick it up as you go, and I give you a very brief overview later in this chapter to get you started. I’ll be calling out the major XAML concepts as I use them. 1 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd What Software Do You Need for This Book? You need two things for Windows app development: a PC running Windows 8 and Visual Studio 2012. You’ll need to buy a copy of Windows 8 if you get serious about Windows app development, but if you are just curious, then you can get a 90-day trial from Microsoft from http://msdn.microsoft.com/en-us/evalcenter. You’ll find what you need by clicking the Release link in the Windows and Platform Development section. You are required to create a Microsoft account to get the evaluation, but you’ll need one of those anyway to get set up as a developer. Microsoft accounts are free to create, and there are instructions to follow if you don’t already have one. ■ Tip You can run Windows 8 in a virtual machine, but for the best results I recommend installing the operating system directly onto a PC. Visual Studio 2012 is Microsoft’s development environment. The good news is that Microsoft makes a basic version of Visual Studio available for free, and that’s the version I’ll be using in this book. It has the catchy name ofVisual Studio 2012 Express for Windows 8, and you can download the installer from www.microsoft.com/visualstudio/11/en-us/ products/express. Several flavors are available, each of which can be used to develop a specific kind of application. For app development, you will need Visual Studio Express 2012 for Windows 8. The names of the different flavors are confusingly similar, so be sure to download the right one. The free version doesn’t have all of the testing and integration features that some of the paid-for versions of Visual Studio contain, but you don’t need those to create Windows apps. In all other respects, the free version of Visual Studio is fully featured and does everything you will need. ■ Tip don’t worry if you have a paid-for version of Visual Studio 2012. You won’t need all of the features in your edition, but all of the instructions and examples in this book will work without any problems or modification. Install Visual Studio as you would any other app. Although the software is free, you will need to activate it, which requires the Microsoft account you created earlier. After you have gone through the process, you’ll end up with a code that you can use to activate Visual Studio. You will also need a developer license, which is free as well. When you first start Visual Studio 2012, you will be prompted to obtain a license; it takes only a second, and, once again, it requires the Microsoft account you created earlier. 2 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd ■ Note Although Visual Studio and the Microsoft account are free, you will have to pay if you want to publish apps to the Windows Store. Microsoft currently charges $49 per year for individuals and $99 per year for companies, but you may be entitled to free publishing if you subscribe to Microsoft packages such as MSdn. What Is the Structure of This Book? I focus on the key techniques and features that make a Windows app. You already know how to write C#, and I am not going to waste your time teaching you what you already know. This book is about translating your C# and XAML development experience into the Windows app world, and that means focusing on what makes a Windows app different and special. I have taken a relaxed approach to mixing topics. Aside from the main theme in each chapter, you’ll find some essential context to explain why features are important and why you should implement them. By the end of this book, you will understand how to build a Windows app that integrates properly into Windows 8 and presents a user experience that is consistent with apps written using other languages, such as C++ or JavaScript. This is a primer to get you started on app development for Windows 8. It isn’t a comprehensive tutorial; as a consequence, I have focused on those topics that are the major building blocks for an app. There is a lot of information that I just couldn’t fit into such a slim volume. If you do want more comprehensive coverage of Windows app development, then Apress has published Jesse Liberty’s Pro Windows 8 Development with XAML and C#. In addition, Apress will publish my Pro Windows 8 Development with HTML5 and JavaScript book if you want to use more web-oriented technologies to build your apps. The following sections summarize the chapters in this book. Chapter 1: Getting Started This chapter. Aside from introducing this book, I show you how to create the Visual Studio project for the example app that I use throughout this book. I give you a brief overview of XAML, take you on a tour of the important files in an app development project, show you how to run your apps in the Visual Studio simulator, and explain how to use the debugger. Chapter 2: Data, Bindings, and Pages Data is at the heart of any Windows app, and in this chapter I show you how to define a view model and how to use data bindings to bring that data into your app layouts. These techniques are essential to building apps that are easy to extend, easy to test, and easy to maintain. Along the way, I’ll show you how to use pages to break your app into manageable chunks of XAML and C# code. 3 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd Chapter 3: AppBars, Flyouts, and NavBars Some user interface controls are common to all Windows apps, regardless of which language is used to create them. In this chapter, I show you how to create and configure AppBars, Flyouts, and NavBars, which are the most important of these common controls; together they form the backbone of your interaction with the user. Chapter 4: Layouts and Tiles The functionality of a Windows app extends to the Windows 8 Start menu, which offers a number of ways to present the user with additional information. In this chapter, I show you how to create and update dynamic Start tiles and how to apply badges to those tiles. I also show you how to deal with the snapped and filled layouts, which allow a Windows 8 user to use two Windows apps side by side. You can adapt to these layouts using just C# code or a mix of code and XAML. I show you both approaches. Chapter 5: App Life Cycle and Contracts Windows applies a very specific life-cycle model to apps. In this chapter, I explain how the model works, show you how to receive and respond to the most life-cycle events, and explain how to manage the transitions between a suspended and running app. I demonstrate how to create and manage asynchronous tasks and how to bring them under control when your app is suspended. Finally, I show you how to support contracts, which allow your app to seamlessly integrate into the wider Windows 8 experience. More About the Example Windows App The example app for this book is a simple grocery list manager called GrocerApp. As an app in its own right, GrocerApp is pretty dull, but it is a perfect platform to demonstrate the most important app features. In Figure 1-1, you can see how the app will look by the end of this book. 4 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd Figure 1-1. The example app This is a book about programming and not design. GrocerApp is not a pretty app, and I don’t even implement all of its features. It is a vehicle for demonstrating coding techniques, pure and simple. Is There a Lot of Code in This Book? Yes. In fact, there is so much code that I couldn’t fit it all in without some editing. So, when I introduce a new topic or make a lot of changes, I’ll show you a complete C# or XAML file. When I make small changes or want to emphasize a few critical lines of code or markup, I’ll show you a code fragment and highlight the important changes. You can see what this looks like in Listing 1-1, which is taken from Chapter 5. Listing 1-1. A Code Fragment ... protected override void OnNavigatedTo(NavigationEventArgs e) { viewModel = (ViewModel)e.Parameter; ItemDetailFrame.Navigate(typeof(NoItemSelected)); viewModel.PropertyChanged += (sender, args) => { if (args.PropertyName == "SelectedItemIndex") { groceryList.SelectedIndex = viewModel.SelectedItemIndex; if (viewModel.SelectedItemIndex == −1) { ItemDetailFrame.Navigate(typeof(NoItemSelected)); AppBarDoneButton.IsEnabled = false; 5 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd } else { ItemDetailFrame.Navigate(typeof(ItemDetail), viewModel); AppBarDoneButton.IsEnabled = true; } } }; } ... These fragments make it easier for me to pack more code into this slim book, but they make following along with the examples in Visual Studio more difficult. If you do want to follow the examples, then the best way is to download the source code for this book from Apress.com. The code is available for free and includes a complete Visual Studio project for every chapter in the book. Getting Up and Running In this section, I’ll create the project for the example app and show you each of the project elements that Visual Studio generates. I’ll break this process down step-by-step so that you can follow along. If you prefer, you can download the ready-made project from Apress.com. Creating the Project To create the example project, start Visual Studio and select File ➤ New Project. In the New Project dialog, select Visual C# from the Templates section on the left of the screen, and select Blank App from the available project templates, as shown in Figure 1-2. Figure 1-2. Creating the example project 6 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd Set the name of the project to GrocerApp, and click the OK button to create the project. Visual Studio will create and populate the project with some initial files. ■ Tip Visual Studio includes some basic templates for C# Windows apps. I don’t like them, and I think they strike an odd balance between XAML and C# code. For this reason, I will be working with the Blank App template, which creates a project with the bare essentials for app development. Figure 1-3 shows the contents of the new project as displayed by the Visual Studio Solution Explorer. In the sections that follow, I’ll describe the most important files in the project. Figure 1-3. The contents of a Visual Studio project created using the Blank App template 7 www.allitebooks.com
CHAPTER 1 ■ GETTInG STARTEd ■ Tip don’t worry if the purpose or content of these files isn’t immediately obvious. I’ll explain everything you need to know as I build the example app. At this stage, I just want you to get a feel for how a Visual Studio app project fits together and what the important files are. Windows apps use a slimmed-down version of the .nET Framework library. You can see which namespaces are available by double-clicking the .net for Windows Store apps item in the References section of the Solution Explorer. Exploring the App.xaml File The App.xaml file and its code-behind file, App.xaml.cs, are used to start the Windows app. The main use for the XAML file is to associate StandardStyles.xaml from the Common folder with the app, as shown in Listing 1-2. Listing 1-2. The App.xaml File <Application x:Class="GrocerApp.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:GrocerApp"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!-- Styles that define common aspects of the platform look and feel Required by Visual Studio project and item templates --> <ResourceDictionary Source="Common/StandardStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> I’ll discuss the StandardStyles.xaml file shortly, and, later in this chapter, I’ll update App.xaml to reference my own resource dictionary. The code-behind file is much more interesting and is shown in Listing 1-3. (For brevity, I have removed some of the comments that Visual Studio adds to this file.) 8
CHAPTER 1 ■ GETTInG STARTEd Listing 1-3. The App.xaml.cs File using System; using System.Collections.Generic; using System.IO; using System.Linq; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace GrocerApp { sealed partial class App : Application { public App() { this.InitializeComponent(); this.Suspending += OnSuspending; } protected override void OnLaunched(LaunchActivatedEventArgs args) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) { // Create a Frame to act as the navigation context // and navigate to the first page rootFrame = new Frame(); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } 9
CHAPTER 1 ■ GETTInG STARTEd if (rootFrame.Content == null) { if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)) { throw new Exception("Failed to create initial page"); } } // Ensure the current window is active Window.Current.Activate(); } private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); //TODO: Save application state and stop any background activity deferral.Complete(); } } } Windows apps have a very specific life-cycle model, which is expressed through the App.xaml.cs file. It is essential to understand and embrace this model, which I explain in Chapter 5. For the moment, you need to know only that the OnLaunched method is called when the app is started and that a new instance of the MainPage class is loaded and used as the main interface for the app. ■ Tip For brevity, I have removed most of the comments from these files and removed the namespace references that are not used by the code in the class. Exploring the MainPage.xaml File Pages are the basic building blocks for a Windows app. When you create a project using the Blank App template, Visual Studio creates a blank page, which it names MainPage. xaml. Listing 1-4 shows the content of the MainPage.xaml file, which contains just enough XAML to display ... well, a blank page. Listing 1-4. The Contents of the MainPage.xaml File <Page x:Class="GrocerApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:GrocerApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> 10
CHAPTER 1 ■ GETTInG STARTEd <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> </Grid> </Page> If you have used XAML before, you will recognize the Grid control. Windows app UI controls work in generally the same way as those from WPF or Silverlight, but there are fewer of them, and some of the advanced layout and data binding features are not available. I’ll create a more useful Page layout in Chapter 2 when I start to build the example project. The code-behind file for MainPage.xaml will also be familiar if you have XAML experience, as shown in Listing 1-5. ■ Tip don’t worry about the XAML and code-behind files for the moment; I provide a quick overview later in this chapter. Listing 1-5. The Contents of the MainPage.xaml.cs File using System; using System.Collections.Generic; using System.IO; using System.Linq; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace GrocerApp { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { } } } 11
CHAPTER 1 ■ GETTInG STARTEd Exploring the StandardStyles.xaml File The Common folder contains files that are used by Visual Studio project templates. The only file in this folder when the Blank App template is used is StandardStyles.xaml, which is the resource dictionary file referred to in the App.xaml file (as shown in Listing 1-2). The StandardStyles.xaml file contains some the styles and templates that make it easier to create an app that has an appearance that is consistent with the broader Windows app look and feel. I am not going to list the complete file because it contains a lot of content, but Listing 1-6 shows an example of a text-related style. Listing 1-6. A Style from the StandardStyles.xaml File ... <Style x:Key="HeaderTextStyle" TargetType="TextBlock" BasedOn="{StaticResource BaselineTextStyle}"> <Setter Property="FontSize" Value="56"/> <Setter Property="FontWeight" Value="Light"/> <Setter Property="LineHeight" Value="40"/> <Setter Property="RenderTransform"> <Setter.Value> <TranslateTransform X="-2" Y="8"/> </Setter.Value> </Setter> </Style> ... ■ Caution don’t edit the files in the Common folder. I’ll show you how to create and reference a custom resource dictionary in Chapter 2. Exploring the Package.appxmanifest File The final file worth mentioning is the manifest, called Package.appxmanifest. This is an XML file that provides information about your app to Windows. You can edit this file as raw XML, but Visual Studio provides a nice properties-based editor to use instead. I’ll return to this file in later chapters to configure some of the app settings. An Incredibly Brief XAML Overview Don’t worry if you haven’t used XAML before. The learning curve for creating Windows apps will be steeper, but you have the advantage of not expecting features from other XAML application types that are not available in Windows app development. At its heart, XAML creates user interfaces declaratively, rather than in code. So, if I wanted to add a couple of button controls to my project, I add some markup to my XAML file, as shown in Listing 1-7. 12
CHAPTER 1 ■ GETTInG STARTEd Listing 1-7. Adding Controls to the XAML Document <Page x:Class="GrocerApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:GrocerApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <Button x:Name="FirstButton" HorizontalAlignment="Center" Click="ButtonClick">Click Me!</Button> <Button Style="{StaticResource TextButtonStyle}" HorizontalAlignment="Center" Click="ButtonClick">Or Click Me!</Button> </StackPanel> </Grid> </Page> The tag name in a XAML element specifies the control that will be added to the layout. I have added one StackPanel and two Button controls to my project. The StackPanel is a simple container that helps add structure to the layout; it positions its child controls in either a horizontal or vertical line (a stack). The Button controls are just what you’d expect: a button that emits an event when the user clicks it. The hierarchical nature of the XML is translated into the hierarchy of UI controls. By placing the Button elements inside the StackPanel, I have specified that the StackPanel is responsible for the layout of the Button elements. Using the Visual Studio Design Surface You can do everything in C# in a Windows app project and not use XAML at all. But there some compelling reasons to adopt XAML. The main advantage is that the design support for XAML in Visual Studio is pretty good and will, for the most part, show you the effect of changes to your XAML files in real time. As Figure 1-4 shows, Visual Studio reflected the addition of the StackPanel and Button elements on its XAML design surface. This isn’t the same as running the app, but it is a broadly faithful representation, and this feature isn’t available for interfaces created in C#. (The buttons are quite small, so I have magnified them in the figure to make them easier to see). 13
CHAPTER 1 ■ GETTInG STARTEd Figure 1-4. Visual Studio reflecting the contents of a XAML file on its design surface Although XAML tends to be verbose, Visual Studio does a lot to make creating and editing it easier; it has some excellent autocomplete features that offer suggestions for tag names, attributes, and values. You can also design an interface by dragging controls from the Toolbox directly onto the design surface and configuring them using the Properties window. If neither of those approaches suits you, there is support for creating the XAML for apps in Blend for Visual Studio, which was installed on your machine as part of the Visual Studio setup. I favor writing the XAML directly in the code editor, but then I am crusty old-school programmer who has never really trusted visual design tools, even though they have become pretty good in recent years. You may not be quite as resistant to change, and you should try the different styles of UI development to see which suits you. For the purposes of this book, I’ll show you changes to the XAML directly. Configuring Controls in XAML You configure Windows app UI controls by setting attributes on the corresponding XAML element. So, for example, I wanted the StackPanel to center its child controls. To do this, I set values for the HorizontalAlignment and VerticalAlignment attributes, like this: ... <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> ... 14
CHAPTER 1 ■ GETTInG STARTEd Applying Styles Attributes are also used to apply styles to UI controls. As an example, I applied the TextButtonStyle that is defined by Microsoft in the StandardStyles.xaml file: ... <Button Style="{StaticResource TextButtonStyle}" HorizontalAlignment="Center" Click="ButtonClick">Or Click Me!</Button> ... There are different ways to define and reference styles in XAML. I have used StaticResource to specify the style I want, but there are options for getting the style information from all sorts of sources. I am going to keep things simple in this book and stick to the basics wherever possible, focusing on the features that are specific to Windows apps. Specifying Event Handlers To specify a handler method for an event, you simply use the element attribute that corresponds to the event you require, like this: ... <Button x:Name="FirstButton" HorizontalAlignment="Center" Click="ButtonClick">Click Me!</Button> ... I have specified that the click event (which is triggered when the user clicks the button) will be handled by the ButtonClick method. Visual Studio will offer to create an event handler method for you when you apply an event attribute; I’ll show you the other side of this relationship in in the next section. Configuring Controls in Code XAML relies on some clever compiler tricks and a C# feature known as partial classes. The markup in a XAML file is converted and combined with the code-behind file to create a single .NET class. This can seem a bit odd at first, but it does allow for a nice hybrid model where you can define and configure controls in XAML, the code-behind C# class, or both. The simplest way of demonstrating this relationship is to show you the implementation of the event handler that I specified for the Button elements in the XAML file. Listing 1-8 shows the MainPage.xaml.cs file, which is the code-behind file for MainPage.xaml. 15
CHAPTER 1 ■ GETTInG STARTEd Listing 1-8. The MainPage.xaml.cs File using System; using System.Collections.Generic; using System.IO; using System.Linq; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; namespace GrocerApp { public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { } private void ButtonClick(object sender, RoutedEventArgs e) { System.Diagnostics.Debug.WriteLine("Button Clicked: " + ((Button)e.OriginalSource).Content); } } } I am able to refer to the ButtonClick method without any qualification in the XAML file because the code generated from the XAML file is merged with the C# code in the code-behind file to create a single class. The result is that when one of the Button elements in the app layout is clicked, my C# ButtonClick method is invoked. ■ Tip no console is available for Windows apps, so use the static System.Diagnostcs. Debug.WriteLine method if you want to write out messages to help figure out what’s going on in your app. These are shown in the Visual Studio Output window, but only if you start your app using Start With debugging from the Visual Studio debug menu. 16
CHAPTER 1 ■ GETTInG STARTEd This relationship goes both ways. Notice that some of the elements in the XAML file have an x:Name attribute, like this: ... <Button x:Name="FirstButton" HorizontalAlignment="Center" Click="ButtonClick">Click Me!</Button> ... When you specify a value for this attribute, the compiler creates a variable whose value is the UI control that was created from the XAML element. This means you can supplement the XAML configuration of your controls with C# code or change the configuration of elements programmatically. Listing 1-9 shows how I change the configuration of the button whose name is FirstButton in the ButtonClick method defined in the MainPage.xaml.cs code-behind file. Listing 1-9. Configuring a Control in Code in Response to an Event in the MainPage.xaml.cs File ... private void ButtonClick(object sender, RoutedEventArgs e) { FirstButton.Content = "Pressed"; FirstButton.FontSize = 50; System.Diagnostics.Debug.WriteLine("Button Clicked: " + ((Button)e.OriginalSource).Content); } ... I don’t have to qualify the control name in any way. In this example, I change the contents of the button and the size of the font. Since these new statements are in the Click event handler function, clicking either button will cause the configuration of the FirstButton to change. That’s all you need to know about XAML for the moment. To summarize: • XAML is converted into code and merged with the contents of the code-behind file to create a single .NET class. • You can configure UI controls in XAML or in code. • Using XAML lets you use the Visual Studio design tools, which are pretty good. XAML may strike you as verbose and hard to read at first, but you will soon get used to it. I find that it is a lot easier to work with XAML than using just C# code, although I admit it took me quite some time with XAML before I decided that was the case. 17
显示前 20 页。下载资料查看完整内容。
暂无评论。快来抢沙发吧!
评论 (0)
暂无评论。快来抢沙发吧!