Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

visual studio 2012 and .net 4.5 expert development cookbook
Nội dung xem thử
Mô tả chi tiết
www.it-ebooks.info
Visual Studio 2012
and .NET 4.5 Expert
Development Cookbook
Over 40 recipes for successfully mixing the powerful
capabilities of .NET 4.5 and Visual Studio 2012
Abhishek Sur
PUBLISHING
professional expertise distilled
BIRMINGHAM - MUMBAI
www.it-ebooks.info
Visual Studio 2012 and .NET 4.5 Expert
Development Cookbook
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be
caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.
First published: April 2013
Production Reference: 1050413
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84968-670-9
www.packtpub.com
Cover Image by Siddhart Ravishankar ([email protected])
www.it-ebooks.info
Credits
Author
Abhishek Sur
Reviewers
Carlos Hulot
Ahmed Ilyas
Sergiy Suchok
Ken Tucker
Acquisition Editor
Kartikey Pandey
Lead Technical Editor
Susmita Panda
Technical Editors
Jalasha D'costa
Amit Ramadas
Project Coordinator
Anish Ramchandani
Proofreader
Claire Cresswell-Lane
Indexer
Tejal Soni
Graphics
Aditi Gajjar
Production Coordinator
Aparna Bhagat
Cover Work
Aparna Bhagat
www.it-ebooks.info
About the Authors
Abhishek Sur is a Microsoft MVP in Client App Dev since 2011. He is an architect in the
.NET platform. He has profound theoretical insight and years of hands on experience in
different .NET products and languages. Over the years, he has helped developers throughout
the world with his experience and knowledge. He owns a Microsoft User Group in Kolkata
named KolkataGeeks, and regularly organizes events and seminars in various places for
spreading .NET awareness. A renowned public speaker, voracious reader, and technology
buff, his main interest lies in exploring the new realms of .NET technology and coming up with
priceless write-ups on the unexplored domains of .NET. He is associated with the Microsoft
Insider list on WPF and C#, and is in constant touch with product group teams. He holds a
Masters degree in Computers along with various other certificates to his credit.
On the web, Abhishek is a freelance content producer, developer, and a site administrator. His
website abhisheksur.com guides both budding and experienced developers to understand
the details of languages and latest technology. He enjoys a huge fan following on social
networks. You can reach him at [email protected], or get online updates from
Facebook or Twitter @abhi2434.
www.it-ebooks.info
Acknowledgement
Writing this book would not have been possible without the help of many people. Knowledge
is a priceless alternative of time. While writing this book, I have spent a considerable amount
of time in reading a lot of online journals, documentations, and blogs to explain and present
concepts and ideas in a more lucid way.
First of all, I am extremely thankful to the entire Packt Publishing team for their continuous
help. I would like to thank Anish Ramchandani and Susmita Panda, who have been very
supportive and helped me to meet the deadlines. My most sincere thanks to my reviewers
Carlos Hulot, Ahmed Illyas, Sergiy Suchok, and Ken Tucker for reviewing every line I wrote
for this book, which resulted in bringing so many positive changes in this book. Last but not
the least, a lot of thanks to Amit Ramadas and Jalasha D'costa for editing the chapters of
this book.
I would also like to thank my colleague cum friends Shibatosh, Ranjit, Pallab, Ayan, Malini, and
other members of our group for their continuous support and motivation to write this book. I
am also thankful for being in touch with Mr. Raj Goswami, CEO and Mr. D. K Goswami, Director
of BuildFusion, India. I would also like to extend my sincere thanks to Anoop Madhusudan,
Abhijit Jana, Kunal Chowdhury, Dhananjay Kumar, Karthikayan Anabarasan, Lohith, Abhishek
Kant, and Shivprasad Koirala for their continuous help and motivation.
I would also like to acknowledge my fiancée Riya for helping me and motivating me to write
better. I want to dedicate this book to her.
Being a community lover I used to write a lot of blogs and articles, but this is the first time
I am putting my experience and efforts in the form of a book. I am really thankful to all my
readers and followers all over the world who loved my blogs and hopefully they will love
this book too.
www.it-ebooks.info
About the Reviewers
Carlos Hulot has been working in the IT area for more than 20 years in different
capabilities, from software development, project management to IT marketing product
development and management. Carlos has worked for multinational companies like Royal
Philips Electronics, PricewaterhouseCoopers, and Microsoft. Currently, Carlos is working as an
independent IT consultant. Carlos is a Computer Science lecturer at two Brazilian universities.
Carlos holds a Ph.D in Computer Science and Electronics from the University of Southampton,
UK, and a B.Sc. in Physics from the University of São Paulo, Brazil.
Ahmed Ilyas has a bachelor's degree in engineering from Napier University in Edinburgh,
Scotland, and has majored in software development. He has 15 years of professional
experience in software development.
After leaving Microsoft, he ventured into setting up his consultancy company offering the
best possible solutions for a magnitude of industries and providing real-world answers
to problems. They only use the Microsoft stack to build these technologies, to be able to
bring in best practice, patterns, and software to their client base. Thus, enabling long term
stability and compliance in the ever changing software industry and also improving software
developers around the globe—pushing the limits in technology as well as develop themselves
to become better.
This went on to being awarded the MVP in C# three times by Microsoft for providing excellence
and independent real-world solutions to problems that developers face.
With the breadth and depth of knowledge he as obtained not only from his research but
also with the valuable wealth of information and research at Microsoft, the motivation
and inspirations come from this, with 90 percent of the world using at least one form of
Microsoft technology.
www.it-ebooks.info
Ahmed Ilyas has worked for a number of clients and employers. With the great reputation that
he has, this has resulted in having a large client base for his consultancy company, Sandler
Ltd (UK) which includes clients from different industries from, media to medical and beyond.
Some clients have included him on their "approved contractors/consultants" list, which
include ICS Solution Ltd and he has been placed on their "DreamTeam" portal and also CODE
Consulting/EPS Software (www.codemag.com, based in the US).
Ahmed Ilyas has also been involved, in the past, in reviewing books for Packt Publishing and
wishes to thank them for the opportunity once again.
I would like to thank the author/publisher of this book for giving me the
great honor and privilege in reviewing the book. I would also like to thank
my client base and especially Microsoft Corporation and my colleagues over
there for enabling me to become a reputable leader as a software developer
in the industry, which is my passion.
Sergiy Suchok graduated in 2004 with honors from the Faculty of Cybernetics, Taras
Shevchenko National University of Kyiv (Ukraine), and has since then been keen on
information technology. He currently works in the banking area and has a PhD in Economics.
Sergiy is the coauthor of more than 45 articles and has participated in more than 20 scientific
and practical conferences devoted to economic and mathematical modeling. He is a member
of the New Atlantis Youth Public Organization (newatlantida.org.ua) and devotes his
leisure time to environmental protection issues, historical, and patriotic development and
popularization of a grateful attitude towards the Earth. He also writes poetry and short stories
and makes macramé.
I would like to thank my wife and my young daughter for their patience and
understanding while reviewing.
Ken Tucker is a web developer for Sea World, and has been a Microsoft MVP since October
2003. In his spare time he enjoys writing Windows Phone and Windows Store apps.
www.it-ebooks.info
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to
your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
files available? You can upgrade to the eBook version at www.PacktPub.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
[email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up
for a range of free newsletters and receive exclusive discounts and offers on Packt books
and eBooks.
TM
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can access, read and search across Packt's entire library of books.
Why Subscribe?
f Fully searchable across every book published by Packt
f Copy and paste, print and bookmark content
f On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view nine entirely free books. Simply use your login credentials for
immediate access.
Instant Updates on New Packt Books
Get notified! Find out when new books are published by following @PacktEnterprise on
Twitter, or the Packt Enterprise Facebook page.
www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: Introduction to Visual Studio IDE Features 7
Introduction 7
Identifying the various components of Visual Studio IDE 8
Working with Solution Explorer and Class View 14
Working with the main workspace area of IDE 18
Navigating between code inside the IDE 22
Extending Visual Studio templates 29
Using Code Snippets in Visual Studio 39
Using Smart Tags and Refactor in Visual Studio 45
Chapter 2: Basics of .NET Programs and Memory Management 51
Introduction 52
Inspecting the internal structure of a .NET assembly 53
Working with different types of assemblies 60
Inspecting the major components of a .NET program 70
How to work with custom configurations for an application 75
How to disassemble an assembly 81
Securing your code from reverse engineering by using obfuscation 89
Understanding .NET garbage collection and memory management 96
How to find memory leaks in a .NET program 104
Solutions to 10 common mistakes made by developers while writing code 115
Chapter 3: Asynchronous Programming in .NET 123
Introduction 123
Introduction to Threading and Asynchronous Threading patterns 125
Working with Event-based asynchronous pattern and BackgroundWorker 135
Working with thread locking and synchronization 139
Lock statement using task-based parallelism in concurrent programming 147
www.it-ebooks.info
ii
Table of Contents
Working with async and await patterns 155
Working with Task Parallel Library data flows 165
Chapter 4: Enhancements to ASP.NET 173
Introduction 173
Understanding major performance boosters in ASP.NET web applications 174
How to work with statically-typed model binding in ASP.NET applications 185
Introduction to HTML5 and CSS3 in ASP.NET applications 189
Working with jQuery in Visual Studio with ASP.NET 205
Working with task-based asynchronous HttpHandlers and HttpModules 211
New enhancements to various Visual Studio editors 214
Chapter 5: Enhancements to WPF 223
Introduction 223
Getting started with WPF and its major enhancements in .NET 4.5 226
Building applications using MVVM pattern supported by WPF 234
Using the Ribbon User Interface in WPF 260
Using WeakEvent pattern in WPF 272
Chapter 6: Building Touch-sensitive Device Applications
in Windows 8 275
Introduction 275
Building your first Windows 8 style tiles application
using JavaScript, HTML5, and CSS 278
Writing a library for WinJS 294
Building your first Windows 8 style tiles application using C# and XAML 297
Working with storage files in Windows 8 style tiles applications 306
Understanding the application life cycle of WinRT applications 312
Chapter 7: Communication and Sharing using Windows 8 325
Introduction 325
How to enable app to app sharing inside a Windows 8 environment 326
Working with notification and services 330
How to perform background transfers of data in Windows 8 style tiles
applications 348
Index 355
www.it-ebooks.info
Preface
Moving through the last few decades, from being the new kid on the block, .NET has turned
itself into the most talked about young thing on the IT horizon. More and more people are
getting inclined to use .NET, to make a mark in their career and also, more and more, clients
are coming to .NET to accomplish their dreams. The recent buzz about C# being the best
programming language has now made the world of developers very eager to build .NET
applications. As the crowd is growing in the developers arena, there is always a need to
have a clear perception on how things work and ways to make things better.
Visual Studio 2012 and .NET 4.5 Expert Development Cookbook mainly focuses on things
that you need to know in those crunch situations as a developer. It also tries to feed you
with as much details as it can, and covers as much technological domain in the world of
.NET. The book is written in the form of recipes with step-by-step tutorials on every topic
where the developer accompanies the author in this wonderful journey into the known and
hitherto unknown realms of .NET. The recipes in the book, which are mostly sought out by
developers on the Internet, are chosen in such a way so as to practically demonstrate them
and not restrict developers only to them, but to also expose the other unexplored domains on
the same topic to give them a clear view of the whole picture. There is a special section for
each recipes bearing the heading There's more…, which always focuses on giving you extra
knowledge on things that you might have missed without it. By the time you come to the end
of this journey, you will feel the comfort and enjoy the confidence that a clear understanding
of the insight of .NET gives you.
The book is a practical handbook that could give you optimal utilization of time for knowledge.
It separately presents the topics very precisely and elaborates the same which you can rely
on for a deeper look. It explains the recipes with proper sample code blocks that might make
the usage of each topic very clear and make you utilize the final source code while writing your
real-world applications. The examples taken for this book will clear your understanding of how
things exactly work for that particular recipe and also adapt you as a developer to make use
of the same source code in your production environment efficiently and quickly. If you want to
utilize your busy schedule to explore all the necessary ongoing technology in the market, this
book is best suited for you.
www.it-ebooks.info
Preface
2
The book focuses on giving you:
f Maximum utilization of time for learning
f Major insights into ongoing technologies such as Visual Studio 2012, .NET 4.5, ASP.
NET, Windows 8 Applications, Windows Presentation Foundation, HTML5, jQuery,
memory management, and so on
f Practical examples on procedures to create real-world applications
f Step-by-step examples to create simple applications based on heads
What this book covers
Chapter 1, Introduction to Visual Studio IDE Features, starts with a basic introduction to Visual
Studio IDE and gives the developer insights into how to increase productivity of development
using a common set of tools and features present inside the IDE.
Chapter 2, Basics of .NET Programs and Memory Management, introduces the intersection
of a .NET program and its core components. It dives deep in demonstrating the .NET
infrastructure with detailed explanation of memory management and related techniques.
Chapter 3, Asynchronous Programming in .NET, focuses on introducing all existing techniques
to deal with threading in .NET followed by the newer patterns that takes over the existing
working principles with in-depth explanation on their working principles.
Chapter 4, Enhancements to ASP.NET, gives you an introduction to latest enhancements of
ASP.NET 4.5 with HTML5 and jQuery. It also introduces some of the performance boosters
available in .NET 4.5 and Visual Studio 2012 with ASP.NET.
Chapter 5, Enhancements to WPF, introduces the enhancements to WPF 4.5 and the major
components of WPF. It gives a practical implementation of MVVM based WPF application
covering all the facets required to program in WPF environment.
Chapter 6, Building Touch-sensitive Device Applications in Windows 8, introduces the new
programming model for developing Windows 8 style tiles application. It gives a step-by-step
introduction in how to program using HTML5 and JavaScript as well as WPF and C# for
developing Windows 8 applications.
Chapter 7, Communication and Sharing Using Windows 8, focuses on how to implement
network-enabled applications in Windows 8 with step by step implementation on how sharing
and searching works inside the Windows 8 environment.
Appendix, .NET languages and its Construct, focuses on giving insights on how languages
work in the .NET framework and C# with details explanation with examples of various features
of C# language.
You can download this Appendix from http://www.packtpub.com/sites/default/
files/downloads/6709EN_Appendix_NET_Languages_and_its_Construct.pdf
www.it-ebooks.info
Preface
3
What you need for this book
The basic software requirements for this book are as follows:
f Microsoft .NET Framework 4.5 and higher
f Microsoft Visual Studio 2012 Express or higher editions
f Windows 8 Operating System (especially to work with Chapter 6 and 7)
f Latest web browsers
Who this book is for
The purpose of this book is to give you ready made steps in the form of recipes to develop
common tasks that, as a developer, you might often be required to access. The book utilizes
its chapters skillfully to provide as much information as it can and also with as much detail
as necessary to kick start the subject. The book also delivers in-depth analysis of some
advanced section of the subject to get you to expertise level. If you are a starter in the
development environment and want to get expertise on ongoing technologies in the market,
this book is ideal for you. Even for architects and project managers, the book can be a guide
to enrich their existing knowledge.
The book uses C# and Visual Studio 2012 with Windows 8 (as the operating system) in the
examples. Even though the book does not require any knowledge to start, it expects some
basic theoretical and practical overall experience on the subjects to understand the recipes.
The book bridges the gap between a normal developer to an expert architect.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of
information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "The requestValidationMode attribute
of httpRuntime defines how the request is validated to the server before calling
HttpPipeline."
A block of code is set as follows:
(function($){
$.fn.extend({
Value1 : 20,
myMethod : function(msg){
alert(msg + "value : " + this.Value1);
}
});
})(JQuery);
www.it-ebooks.info
Preface
4
Any command-line input or output is written as follows:
Install-Package Microsoft.Web.Optimization
New terms and important words are shown in bold. Words that you see on the screen, in
menus or dialog boxes for example, appear in the text like this: "you can also open Nuget
package manager by right-clicking on the references folder of the project and select Add
Library Package Reference".
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—
what you liked or may have disliked. Reader feedback is important for us to develop titles that
you really get the most out of.
To send us general feedback, simply send an e-mail to [email protected], and
mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or
contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to
get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your
account at http://www.packtpub.com. If you purchased this book elsewhere, you can
visit http://www.packtpub.com/support and register to have the files e-mailed directly
to you.
www.it-ebooks.info