Sunday, March 25, 2012

Windows Phone - Logger

Hello

Logging is very important for development and for production.

Logger

The logger i have found is NLog .

It can be downloaded here.

Adding to project

Next you need to add configuration file by Add->New Item and choosing "Empty NLog Configuration File" :


This will add a configuration file name NLog.config and a reference to NLog.dll.

Next  change  NLog.config properties "Copy to Output Directory" to "Copy always"



Logging API


Most important classes are Logger and LogManager.
Logger class represent the name of the logger and has methods for writing to the log.
LogManager create and manage the Logger instances.

You create log in the following way :
 Logger logger = LogManager.GetLogger("someLogName");

The following log level are supported :

  • Trace - relevant in general for development
  • Debug - not enabled for production
  • Info - information
  • Warn - Warning
  • Error - Errors
  • Fatal - Fatal Error

Writing to Log file :
logger.Log(LogLevel.Error, "Some Error - {0}",i);
LogLevel has all log level described above.

Supported Targets

  • Console
  • Memory - store trace in memory
  • MethodCall - call a user defined method
  • Network,NLogViewer,Chainsaw - Network using HTTP , HTTPS
  • WebService - using SOAP,POST
  • LogReciverService - use a WebService using WCF



Configuration file :
The are two important sections :

targets

  • Can have one of more target tag
  • possible target attributes :
  • name
  • xsi:type 


rules

  • Can have one of more logger tags
  • possible rule attributes :
  • name
  • minlevel
  • writeTo

for example


Here all loggers starting level Trace are written to Console.

Remark :

  • Set  EnableConsole=1  under  [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\XDE]  in case you want to use Console target (32 bit PC)


Sample source
NLog_NK.zip

This sample illustrate a usage of the logger with console target.
Add NLog.config as described before.
Click the button to produce log events


MainPage.xaml.cs
NLog.config


Run the application and click the button to produce




Sample source 

NLog_OutputWindow_NK.zip

This sample illustrate a usage of the logger with MethodCall target to write to the Output window.

Add NLog.config as described before.
Click the button to produce log events



MainPage.xaml.cs 



NLog.config


MyClass.cs
Run the application via the debugger, open the Output windows and click the button to produce :



Nathan

Tuesday, March 6, 2012

Windows Phone - Pivot and Panorama (15)

Hello

Hierarchy


Object

  • DependencyObject
  • UIElement
  • FrameworkElement
  • Control
  • ItemsControl 
  • TemplatedItemsControl
  • Panorama
  • Pivot



Pivot and Panorama :

  • Controls
  • Used in case you have to display info which is larger then the screen. 
  • Have a virtual space that is arranged horizontally and can be accessed by sweeping your fingers right or left across the screen
  • Have Title property and TitleTemplate
  • Defines SelectionChanged event and properties SelectedIndex , SelectedItem which relates to the item shown.
Pivot

  •  A collection of PivotItem objects.
  • Tapping on the next header navigate to it


Panorama



PivotItem and PanoramaItem

  • Have a content which is object , in particular other control.
  • Have a Header property and HeaderTemplate


PivotItem

  • Derived from ContentControl.


PanoramaItem 

  • Derived from ContentControl 




Source sample
PivitandPanorama_NK.zip


Add a new page and choose "Windows Phone Panorama Page"








Basically what you get is the following




I have changed it to make it more interesting - all PanoramaItems are images

PanoramaPage.xaml


Note that all three XAML representation of Image element are equivalence.
Remember that PanoramaIten derives from ContentControl so you can dismiss the Content tag

Run the application and click "Panorama Control" button to produce :


Now drag the image to the right to produce








Source sample

PivitandPanorama_NK.zip


Add a new page and choose "Windows Phone Pivot Page"




Basically what you get is the following






I will  change it as i did in the previous so you will notice the difference
PivotPage.xaml



Run the application and click "Pivot Control" button to produce :



Now drag the image to the right to produce


The feeling is very similar to the Panorama control. Panorama seems more smooth

Nathan

Monday, March 5, 2012

Windows Phone - Items Control (14)

Hello

ItemsControl is used to represent collection of objects.

ItemControl has the following hierarchy

Object

  • DependencyObject
  • UIElement
  • FrameworkElement
  • Control
  • ItemesControl
  • Selector
  • ListBox
  • ComboBox

  • TemplatedItemsControl
  • Panorama
  •  Pivot


Important properties of ItemControl



Source sample
ItemsControl_NK.zip


This sample uses two controls

Both control represent a list of sorted font files names which are entered via the code.

StaticItemsControl.xaml


StaticItemsControl.xaml.cs




Run the application and click the button "Static ItemsControl" to produce




Note that you can scroll the ListBox by pointing on it, click the mouse and drag it up or down, but not the ItemsControl, you need ScrollViewer for this.


Customize item display


We saw before  that DataTemplate can be used to render object which have no visual representation. Here i will show how each item in  a list of SolidColorBrush will be given a visual represntation of  :

  • TextBlock with Text as the ARGB of the brush color
  • Rectangle filled with the brush color

Source sample
ItemsControl_NK.zip


CustomizeItemDisplay.xaml



Run the application and click the button "Customize Item Display" to produce





ListBox Selection
ListBox derives from Selector which allows to select an object from a collection.

Selector has a property name SelectedIndex which is the index of the selected item or -1 if no item is selected. Item[SelectedIndex] is equal to the SelectedItem in case item was selected.
SelectedItem is backed by a dependency property so it can function as binding target.

SelectionChanged occurs when selection is changed.


Source sample
ItemsControl_NK.zip


This sample is built over the previous with one change - a TextBox was added . The TextBox Forground is binded to the ListBox.SelectedItem. The SelectedIndex is 0 to start with

Selection.xaml





Run the application and click the button "Selection" , select a color to produce :





Notice that change in the selected color cause a change in the text forground 

Binding to ItemSource

ItemSource :
  • A property of ItemControl
  • It is used to set the content of the ItemsControl
  • Type is IEnumerable

In case the collection is changing dynamically then the collection should implement INotifyCollectionChanged e.g. ObserveableCollection




Sample source
ItemsControl_NK.zip

The sample show a collection of person and allow adding person to the collection. The person added is updated on the GUI.

The implementation includes :

  • Resource name oPersons - ObservableCollection of CPerson
  • Resource  name textBoxStyle - style for TextBlock
  • Resource name textBlockDataTemplateName - DataTemplate for the person name
  • Resource name textBlockDataTemplateFamily - DataTemplate for the person family
  • oListBoxName - ListBox of person name
  • oListBoxFamily -   ListBox of family name
  • Add button - Add person name and family


Two binding here :

  • oPersons - source is binded to oListBox - target. Note that oPersons can not be target because it is not dependency object
  • ListBox item - source is binded to the TextBox - target 

ItemSource.xaml

Resources :
remark - due to some bug in VS2010 i had to use x:Key and not x:Name when i defined oPersons

ContentPanel :


Person.cs
Persons.cs

Remember the collection must be ObservableCollection to notify changes like adding item


Run the application and click the button "Selection" , select a color to produce :







Enter a name - Diego family Forlan and click Add to produce





Nathan