View
219
Download
1
Category
Preview:
Citation preview
Contents ❸• Efficient use of C#
– Asynchronous programming– LINQ
• Visual Studio 2013 and Blend 2013• Source control in cloud• XAML tips
– Margins and contracts– Localizing applications
• Assignments• Questions and answers
Efficient use of C#• C# is a powerful and versatile programming
language• Several modern features
– Such as generic data types, support for asynchronous programming and parallelism, integrated query language etc.
• We will now explore some useful features of C#
Asynchronous programming• Asynchronousness in programming means
that two or more functionalities are executed parallel to each other (simultaneously)
– Not in a queue as traditionally done• Asynchronousness can be used to resolve a
situation where an application seems to freeze– for example when downloading a large file
from the Internet
C# and asynchronousness• C# makes asynchronous programming easy
with the new async and await keywords• It makes extensive use of the Task class which
is a part of Windows 8 class libraries
Sample codeprivate async void StartButton_Click(
object sender, RoutedEventArgs e)
{
try
{
Task<int> intTask = ExampleMethodAsync();
ResultsTextBox.Text += "Doing something else " +
"while the method is processed";
int intResult = await intTask;
ResultsTextBox.Text += String.Format(
"Length: {0}\n\n", intResult);
}
catch (Exception) { //... }
}
LINQ• Short for Language Integrated Query• LINQ is a query language integrated into C#. It
can be used to make fetching data from different sources easier
• Syntax resembles SQL– If you already know SQL, LINQ is easy to learn
Sample LINQ queryint[] luvut = {5,3,9,7,1,6,10,4,2};var lajiteltuna = from l in luvut where l > 5 orderby l select l;foreach (int luku in lajiteltuna){ // display number on screen... }
LINQ-supported data sources• The last example demonstrated a LINQ query
that fetches number from an array• In addition to arrays and application-internal
objects, LINQ can fetch data from SQL databases
– Other supported sources include XML files and web sources
Second exampleNorthwindEntities entities = new NorthwindEntities();
var FinnishClients = from c in entities.Customers
where c.Country == "Finland"
orderby c.CompanyName
select c;
foreach (var client in FinnishClients)
{
int ordercount = client.Orders.Count;
// change processing...
}
Visual Studio 2013 and Blend• Visual Studio and Blend are sister tools. You will need
both to develop Windows 8 applications– Both have been designed to ensure effortless cooperation
between the two applications
• Once you have opened your project in Visual Studio, you can easily switch to Blend to edit any XAML file
– Fastest way to switch is clicking the "Open in Blend" command from Solution Explorer's quick menu
Source control• Source control is an intrinsic part of any software
project• Source control makes it possible for several developers
to keep track of and organize simultaneous changes– Changed or removed code can be retrieved if
needed• Every developer should know the basics of source
control systems
Microsoft's solution• Visual Studio works well together with
Microsoft's own source control system• Team Foundation Service, or TFS for short• A cloud service used directly from Visual
Studio or with web browser
Using TFS through Visual Studio• During registration you can pick a name for
your service– This name will be used to create a URL address
for your project, which will be entered to VS• Connection created in Visual Studio Team
Explorer– Once connected, you can continue to use source
control without additional definitions
CodePlex• CodePlex is an open web service for
communal development of open source solutions
• CodePlex includes a lite version of a TFS-based source control system
• CodePlex has tens of thousands of active users and many interesting projects
XAML tips• Windows 8 interface are defined in the XAML
language• Being familiar with XAML's features helps your
develop better applications faster• Here are three tips for better XAML
programming
Component margins• In many cases the size and positions of used
interface components is defined with the Margin value
• Margin values are relative and dependent on whether your selected component is inside another component
Example• The Margin value will look like this in XAML
code:<Rectangle Margin="7,7,40,20" />
• The Margin value consists of four values, which each represent the distance of the component's each edge to the edges of the mother element
– The values are ordered as: left, top, right, bottom
Contracts• Windows 8 application can use contracts to
connect to the operating system's features• With contracts, the operating system and
applications can exchange data– Certain basic processes don't need to be
executed separately in each application
Search contract• An example of such a connection is the
Windows search function• Search contract adds your application's data
into the results when the user uses the operating system's search feature
– The content may be text, images, videos etc.
Localizing applications• If your application or game is intended to be
distributed outside Finland, you should consider whether it is necessary to translate it to different languages
• Windows 8 applications can be translated to several languages with little effort
Two facets of translation• There are two different facets to translation, called
localization and globalization• Localization
– translating and fitting the application to the target culture (e.g. colors and icons, word play in games, etc.)
• Globalization– An application available only in Finnish or English is set
to support different date, currency, and number separators without translating the interface
Finding used languages• The user's regional and language settings can
be found out with right code strings• Library classes
– Windows.Globalization.Language – System.Globalization.CultureInfo
Examplestring keyboard = Windows. Globalization.Language. CurrentInputMethodLanguageTag;
string operating system = System. Globalization.CultureInfo. CurrentCulture.Name;
Translatings the interface• To translate your interface to different
languages, you should use resource files– Resource files function so that instead of
inputting the text displayed by TextBlocks and Buttons into your XAML code, the text is written into a resource file for each language.
• Resource files can be recognized in Visual Studio Solution Explorer from the .resw file format
Resource file location• Resource files are saved in your project's
Strings sub-folder – You may need to create this folder manually
• More sub-folders are created into this folder for each language code
– Language codes are titled as ”xx-XX”– For example, the subfolder for Finnish would be
called ”fi-FI”
Editing resource files• Resource files are edited in Visual Studio in Excel-like
table, with a key on the left and value on the right– You can copy-paste the values into Excel and send
this file to a translations service provider• The key consists of two parts: first, a unique name
(for example the component's name), and the property name separated with a period. This latter name will be localized.
– For example, ”Button.Content”
Localized XAML definitions• In order for localized resource files to work, each
interface component needs an Uid attribute• Note that the original definition can be left in the
XAML code, such as a button's displayed text– When the application is run, Windows'
application libraries can load the resource files corresponding with the user's regional settings and display the interface based on these values
Example<Button x:Uid="MyButton" Content="Button" />
Uid value
Attribute to localize
The Name property tells us the selected component's Uid value and the property to be localized.The format is ”component.property”.
Assignments1. Find examples of situations where asynchronousness help
Windows 8 applications.2. Blend is suitable for developing a Windows 8 application's
graphics, but it is not a photo editor. Find out what common file formats Blend support to import graphics from other applications such as Photoshop.
3. 3. Register an open source project in CodePlex with your friends, class mates, or colleagues, and share it with the whole world. What did you notice?
Questions and answers 1The LINQ query language seems interesting. What can I use it for?• n short, LINQ and the related libraries are very versatile and
suitable for many tasks.• As evident in the above examples, LINQ can be used to
fetch, filter, and categorize data from many sources.• These sources can include your application's internal
objects (such as tables and lists), SQL databases, XML files etc.
Questions and answers 2I'm trying to use the "await" C# keyword, but the compiler displays an error in the method the "await" command is in.• Most likely the error is caused by forgetting the "async"
keyword in the method definition.• If your have defined the method as, for example,
"private void Button_Click", you must add the "async" keyword after the word "private": ”private async void Button_Click”.
Recommended