ENTERPRISE

Windows 8 Architecture from a Developer’s Point of View : Understanding Windows Runtime (part 3) - Metadata in Windows Runtime - Namespaces

2/26/2014 2:49:47 AM

Namespaces

As you experienced in the previous exercise, Windows Runtime types have hierarchical names. For example, the enumeration with the resolution scale values has the full name of Windows.Graphics.Display.ResolutionScale. The last tag of this name is the type’s simple name, and prefix tags form the namespace hierarchy. The top-level namespace is Windows. It has an embedded namespace, Graphics, and Graphics embeds Display.


NOTE .NET, Java, and C++ programmers are familiar with the concept of namespaces. Windows Runtime uses the namespace with exactly the same semantics as applied in .NET.

Namespaces are important concepts when managing the vast amount of types you utilize while creating your applications. If you had only type names poured into a big pool, it would be very difficult to find them and guess what type is the appropriate one for a certain task.

Another issue would be naming types. You cannot guarantee that no one else uses the same type name that you use. For example, when you name your type Circle, there is a great likelihood that someone else will use the same name. If you buy a package of custom UI components, there already may be a Circle type. How will your application know which Circle type to use at a certain location of the source code, and whether you intend to use your own Circle or the purchased one?

Namespaces are great constructs that help you to group objects into categories. Using well-designed namespace hierarchies makes you more productive, because you can find appropriate types for a certain task easily. For example, when you are about to display images on the screen, first you will look them in the Windows.Graphics.Imaging namespace, because its name suggest that such types exist there.

Namespaces also help you avoid conflicting type names. If you put your own types into their own namespaces (for example, you put Circle into the MyCompany.Shapes namespace), they won’t clash with types from other programmers or companies.

Types can have pretty long full names. Fortunately, all programming languages that manage the concept of namespaces offer some kind of constructs to avoid full names, and allow writing only the simple names. Let’s take a look at a few examples.

C# offers the using clause to help resolve type names:

using Windows.UI.Xaml;

namespace MyAppNamespace
{
class MyClass: UserControl
{
public MyClass()
{
// ...
selectedItem = this.FindName(itemName) as ListBoxItem;
// ...
}
}
}

Visual Basic offers the same construct with the Imports keyword:

Imports Windows.UI.Xaml

Namespace MyAppNamespace

Class MyClass
Inherits UserControl

Public Sub New()
' ...
selectedItem = TryCast(Me.FindName(itemName), ListBoxItem)
' ...
End Sub
End Class
End Namespace

It is not surprising that C++ offers the same concept with the using namespace clause, too:

using namespace Windows::UI::Xaml;

namespace MyAppNamespace
{
class MyClass: UserControl
{
public MyClass()
{
// ...
selectedItem = dynamic_cast<ListBoxItem^>(this->FindName(itemName);
// ...
}
}
}

The using, Imports, and using namespace constructs in C#, Visual Basic, and C++, respectively, instruct the compiler that type names should be looked up in the specified namespaces. This mechanism allows writing only ListBoxItem type names in your programs, because the compiler will check the Windows.UI.Xaml namespace as well. Otherwise, you would have to write the full Windows.UI.Xaml.ListBoxItem name.


NOTE Of course, objects in Windows Runtime namespaces can be accessed from JavaScript programs, too.
Other  
  •  Windows 8 Architecture from a Developer’s Point of View : Windows 8 Development Architecture
  •  Windows 7 : Programming KMDF Hardware Driver - Mapping Resources - Code to Map Resources
  •  Windows 7 : Programming KMDF Hardware Driver - Handling Interrupts (part 2) - Deferred Processing for Interrupts
  •  Windows 7 : Programming KMDF Hardware Driver - Handling Interrupts (part 1) - Code for EvtInterruptIsr Callback
  •  Windows 7 : Programming KMDF Hardware Driver - Support Device Interrupts (part 2) - Post-Interrupt Enable and Pre-Interrupt Disable Processing
  •  Windows 7 : Programming KMDF Hardware Driver - Support Device Interrupts (part 1) - Creating an Interrupt Object,Enabling and Disabling Interrupts
  •  Microsoft Exchange Server 2010 : Managing Data and Database Availability Groups - Content Indexing
  •  Microsoft Exchange Server 2010 : Creating and Managing Database Availability Groups (part 5) - Switching over Servers and Databases
  •  Microsoft Exchange Server 2010 : Creating and Managing Database Availability Groups (part 4) - Configuring Database Availability Group Properties
  •  Microsoft Exchange Server 2010 : Creating and Managing Database Availability Groups (part 3) - Managing Database Availability Group Networks
  •  
    Top 10
    Review : Sigma 24mm f/1.4 DG HSM Art
    Review : Canon EF11-24mm f/4L USM
    Review : Creative Sound Blaster Roar 2
    Review : Philips Fidelio M2L
    Review : Alienware 17 - Dell's Alienware laptops
    Review Smartwatch : Wellograph
    Review : Xiaomi Redmi 2
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8