Wednesday, 24 October 2012

Dependency Properties in WPF

We have already alluded to dependency properties, albeit in a Silverlight context. Now we talk about it from the WPF perspective (not too different).

Wednesday, 17 October 2012

Saving sln files in Visual Studio - UTF-8, Unicode or ANSI?

SAY NO TO ANSI

Anything but ANSI - that's 8 bits and can only hold a limited number of characters (2^8 = 256).

To Update an ObservableCollection on a Background Thread or Not To?

ADD, REMOVE, REPLACE - NO
MODIFY - YES

Modifying an ObservableCollection will lead to an event being raised, which is handled on the same thread as the modification. If you add or remove items from an ObservableCollection from a background thread, the event gets raised before the Add/Remove gets completed.

If you are just changing properties in the elements (trivial updates) you should be fine. The processing can happen in the background, and the trivial update can happen on the main thread, no problems. GUI response time should not be impacted.

protected set

READ ONLY VIEW MODEL FIELD IN BOUND GUI

Making a property setter protected means the property will be non-editable in a GUI control when the data is being used as a ViewModel in the context of data binding.

Domain-neutral Assemblies

BUILD CLEAN SEVERAL TIMES TO CLEAR APPDOMAIN SECURITY ERRORS

What are domain-neutral assemblies?

Domain-neutral assemblies are assemblies designed to work across appdomains. They wil be jitted only once. The idea is to get performance gains in multiple appdomain scenarios. mscorlib is always domain-neutral.

Why are they important?

They are important to understand error messages that come from app domain security messages e.g. on why a particular assembly cannot be loaded when trying to run an application.

Refactoring WPF (IOException: Cannot Locate Resource)

CHECK STARTUPURI

Minimal Structure WPF

What is the minimal structure of a WPF Application, for example, when you go into Visual Studio and click to create a new WPF Application?

This is a guide to a bare-bones WPF application. In fact, you can happily write WPF applications without knowing the bare-bones, but at some point it's essential to look back and fully understand what's going on in order to move forward.

App.xaml, App.xaml.cs and StartupUri (VIRF = Very Important when Refactoring)

Look at App.xaml and App.xaml.cs. This represents a single logical unit, representing the Application (or more precisely a subclass of System.Windows.Application) implemented over two partial classes.

This is like Global.asax in ASP.NET land. It's where you can put shared resources to be shared across multiple windows. It also specifies (in the App.xaml "header element" the StartupUri which will be set to the auto-generated MainWindow.xaml). If you change the StartupUri to something that doesn't except (e.g. PainWindow.xaml instead of MainWindow.xaml you will get an IOException ("Cannot locate resource: PainWindow.xaml"). This can happen after a refactor where you can't decide the name for the MainWindow.xaml and then forget to change the StartupUri (or MSVS fails to change the StartUri automagically - it happens).

App.xaml.cs purports to be the "interaction logic" for App.xaml but in reality it is usually left empty. App.xaml is frequently underutilised in WPF projects and should be given more a higher profile by WPF developers.

MainWindow.xaml and MainWindow.xaml.cs

This contains the window XAML and the IntializeComponent code after which you can define your own initialization logic (e.g. creation of ViewModels etc).

Furthering your Understanding

To understand more completely what a WPF Application actually is, one can read the MSDN document describing System.Windows.Application and its parent classes.

Psychology of XAML

DON'T KNOCK XAML, LEARN IT

Reams and reams of XAML, long XML-based definitions of ControlTemplates and VisualStateManager and StoryBoards, may seem daunting, incomprehensible and unmanageable at first. However, this is a great advancement in computing - as declarative GUI-related information has been isolated and sectioned away from the main imperative and application-related coding. The only time you need to enter into the structured chaos that is XAML is when you need to.

Templates, Templates Everywhere, But what about Controls?

Control Templates Introduced

We have seen DataTemplates in action in a previous post. But controls, too, have a great templating feature. This is known as Control Templates. You MUST, as a WPF MASTER, be completely comfortable with Control Templates. As a LEARNER, you MUST seek every opportunity to use them and become proficient in their use.

Where is and What it does

First go to the Sir Winston Churchill namespace (the "alt-name" for System.Windows.Controls). Here you will find the ControlTemplate class, that specifies the visual structure and behavioural aspects of a Control that can be shared across multiple instances of the control. Not quite control arrays, but you get the picture.

A quote from Sir Winston Churchill might inspire further WPF-learning and indeed create the appetite for greater changes in the toolkit, and indeed might even be interp'ed as a commentary on the toolkit itself, "To IMPROVE is to change, to be PERFECT is to change often".

How is it used?

A control author may specify a default ControlTemplate and an application author may override it.

Changing the Appearance of a Button

A control template can be used to change the visual appearance of a button, making it appear round, for example. This is the canonical MSDN example.

Other types of template to contemplate

CONTROL TEMPLATE - the subject of this post and a wonderful feature of WPF
DATA TEMPLATE
FRAMEWORK TEMPLATE - read all about it here

Tuesday, 16 October 2012

Visual Studio 2012 Welcomes C++ 11

C++ 11 (formerly C++Ox) is the latest version of C++ supported in Visual Studio 2012. New STL headers include <atomic> and <chrono>. The atomic library defines atomic types (data types free from race conditions - so if one thread reads from an atomic item and another writes to it, the behaviour is well-defined). Examples are atomic::int and atomic::long. The chrono library deals with time, specifically objects representing time spans and points in time.

Kinect

Win-Joe has been somewhat remiss in not covering, at least in passing, Microsoft's Kinect technology (where "YOU are the Controller"). The Kinect is an attachment for the Xbox 360 that combines a depth camera (sometimes know "in the trade" as RGB-D), standard RGB camera, motorized tilt and four microphones. MSDN Magazine has a good introduction to Kinect programming.

The October 2012 SDK exposes the infrared stream, allowing image capture in low-light situations. However, it is currently not possible to capture infrared and colour streams simultaneously.The Accelerometer Data APIs now provide information on the sensor's orientation. Desktop robotics - Aloha!

Microsoft UI Automation

This comes bundled with .NET Framework 3.0 as part of WPF. This is covered in the following MSDN magazine article. WPF made its debut in .NET Framework 3.0. Instead of using the GDI as previous libraries did, WPF employed DirectX. The relevant DLL is UIAutomationProvider.

Monday, 8 October 2012

C# Semiotics

We talk about the "lexical structure" of a programming language as if programmers are specialists in linguistics. This is not necessarily the case. Lexical analysis informally relates to "parts of speech", or more generally, parts of sentences. In this case, we are analysing "sentences" in the C# programming language.

Taking a "lexical view" of C# is an interesting exercise and helps to explain programming to semiotics majors, for example.

Take the simple assignment: double pi = 3.14159;

Analysing this lexically we can say:

1. double is an "abstract noun". An abstract noun refers to a concept or idea. In this case, the idea is of a 64-bit floating point value.

2. pi is a "proper noun". A proper noun can refer to a place, object or name of a person. In the above case, it is the name we have given to a specific realisation of the concept, double.

3. the value 3.14159 is a "concrete noun" that refers to the physical object.

The acronym to remember this language of nouns is APC (Abstract, Proper, Concrete).

Language also has the concept of collective nouns. A List of doubles is one example; similar to a gaggle of geese or a flight of swallows or even "a troop of kangaroos".

C# Lexical Structure: Keywords vs Contextual Keywords

How do you extend a language without changing its keywords? Contextual keywords go some way to providing an answer.

An example keyword in C# is "double". Double is a simple type that can store 64-bit floating point values. A contextual keyword only has meaning within a specific context e.g. get/set within a property declaration.

The ascending contextual keyword is used in LINQ queries and only has meaning in LINQ context, for example:

IEnumerable<string> sortAscendingQuery = from elephant in zoo orderby elephant ascending select elephant.

There is no specific word for non-contextual keywords. One might call them, "context-independent" keywords.